当前位置: 首页 > news >正文

【算法与数据结构】513、LeetCode找树左下角的值

文章目录

  • 一、题目
  • 二、解法
  • 三、完整代码

所有的LeetCode题解索引,可以看这篇文章——【算法和数据结构】LeetCode题解。

一、题目

在这里插入图片描述

二、解法

  思路分析:这道题用层序遍历来做比较简单,最底层最左边节点就是层序遍历当中最底层元素容器的第一个值,层序遍历利用了【算法和数据结构】102、LeetCode二叉树的层序遍历文章中的迭代法,稍加修改就可以实现题目要求。
  程序如下

// 层序遍历迭代法
class Solution {
public:int findBottomLeftValue(TreeNode* root) {queue<TreeNode*> que;if (root != NULL) que.push(root);int result = 0;while (!que.empty()) {int size = que.size();  // size必须固定, que.size()是不断变化的for (int i = 0; i < size; ++i) {TreeNode* node = que.front();que.pop();if (i == 0) result = node->val; // 访问容器当中第一个元素if (node->left) que.push(node->left);if (node->right) que.push(node->right);}}return result;}
};

复杂度分析:

  • 时间复杂度: O ( n ) O(n) O(n)
  • 空间复杂度: O ( n ) O(n) O(n)

三、完整代码

# include <iostream>
# include <vector>
# include <queue>
# include <string>
using namespace std;// 树节点定义
struct TreeNode {int val;TreeNode* left;TreeNode* right;TreeNode() : val(0), left(nullptr), right(nullptr) {}TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};// 层序遍历迭代法
class Solution {
public:int findBottomLeftValue(TreeNode* root) {queue<TreeNode*> que;if (root != NULL) que.push(root);int result = 0;while (!que.empty()) {int size = que.size();  // size必须固定, que.size()是不断变化的for (int i = 0; i < size; ++i) {TreeNode* node = que.front();que.pop();if (i == 0) result = node->val; // 访问容器当中第一个元素if (node->left) que.push(node->left);if (node->right) que.push(node->right);}}return result;}
};void my_print1(vector <string>& v, string msg)
{cout << msg << endl;for (vector<string>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << "  ";}cout << endl;
}void my_print2(vector<vector<int>>& v, string str) {cout << str << endl;for (vector<vector<int>>::iterator vit = v.begin(); vit < v.end(); ++vit) {for (vector<int>::iterator it = (*vit).begin(); it < (*vit).end(); ++it) {cout << *it << ' ';}cout << endl;}
}// 前序遍历递归法创建二叉树,每次迭代将容器首元素弹出(弹出代码还可以再优化)
void Tree_Generator(vector<string>& t, TreeNode*& node) {if (t[0] == "NULL" || !t.size()) return;    // 退出条件else {node = new TreeNode(stoi(t[0].c_str()));    // 中t.assign(t.begin() + 1, t.end());Tree_Generator(t, node->left);              // 左t.assign(t.begin() + 1, t.end());Tree_Generator(t, node->right);             // 右}
}int main()
{vector<string> t = { "3", "9", "NULL", "NULL", "20", "15", "NULL", "NULL", "7", "NULL", "NULL" };   // 前序遍历my_print1(t, "目标树:");TreeNode* root = new TreeNode();Tree_Generator(t, root);Solution s1;int result = s1.findBottomLeftValue(root);cout << "最底层最左边元素为:  " << result <<endl; system("pause");return 0;
}

end

http://www.lryc.cn/news/144808.html

相关文章:

  • React——组件缓存 react-activation
  • EV代码签名证书是什么?
  • 融媒行业落地客户旅程编排,详解数字化用户运营实战
  • PDF制作成翻页电子书
  • 多线程
  • BingChat与ChatGPT比较,哪个聊天机器人能让你获益更多?
  • Qt读写ini配置文件(QSettings)、XML
  • JVM知识点(二)
  • 代码随想录算法训练营day44 | LeetCode 518. 零钱兑换 II 377. 组合总和 Ⅳ
  • Vue2向Vue3过度核心技术工程化开发和脚手架
  • Expected all tensors to be on the same device, but found at least two devices
  • Mysql备份命令Mysqldump导入、导出以及压缩成zip、gz格式
  • App卡帧与BlockCanary
  • bpmnjs Properties-panel拓展(ExtensionElements拓展篇)
  • 虚拟机的使用
  • CSS Flex布局
  • Virtual
  • 6、监测数据采集物联网应用开发步骤(5.2)
  • 解释 Git 的基本概念和使用方式
  • 不同ubuntu系统下的不同ros系统可以互相通讯吗
  • 数学建模-模型详解(2)
  • IT运维:使用数据分析平台监控DELL服务器
  • Spring Cloud Alibaba-Sentinel规则
  • go http-proxy
  • 用变压器实现德-英语言翻译【01/8】:嵌入层
  • 【vue3.0中ref与reactive的区别及使用】
  • 计算机竞赛 基于情感分析的网络舆情热点分析系统
  • C++ 动态分配内存|动态数组
  • React Diff算法原理
  • 查局域网所有占用IP