当前位置:网站首页>Leetcode-513:找树的左下角值
Leetcode-513:找树的左下角值
2022-07-03 09:22:00 【ITSOK_U】
题目描述:
给定一个二叉树的根节点root,请找出该二叉树的 最底层 最左边 节点的值。假设二叉树中至少有一个节点。
需要把握的是题中的最左边的意思,因为是最左边,所以按照题目要求我们求的节点不一定就是左叶子节点,也有可能是最深层的一个右叶子,下面是几个例子及其结果
根据上面的图示我们可以很容易的知道求解目标就是树的最深层的最左边节点的值。对于此类的求解问题一般有两个方向:深度优先和广度优先,一般来说对于涉及层次的问题求解来说,广度优先具有更加清晰的逻辑。下面我们分两个角度来分析解决问题。
深度优先DFS(O(N),O(N))
深度优先我们通过递归以先序遍历为基础来实现。为了方便层次的比较我们使用一个引用形参curdepth来记录已求出的最左边的值所处的层次方便更新迭代。另外使用depth来表示当前节点所处的层次,为了确保回溯的正确性(不干扰其他节点的深度更新),depth需要设为一个普通形参。
class Solution {
public:
// 其中num是需要记录返回的值,depth是当前访问的树结点的深度
// curdepth是保存的返回值对应的节点的深度
void getLeft(TreeNode* root, int &num,int depth,int &curdepth){
if(root == nullptr) return;
// 保存每一层的最左边元素
if(root->left == nullptr && root->right == nullptr){
// depth>curdepth就说明当前节点是本层最左边的
if(depth > curdepth){
curdepth = depth;
num = root->val;
}
}
getLeft(root->left,num,depth+1,curdepth);
getLeft(root->right,num,depth+1,curdepth);
}
int findBottomLeftValue(TreeNode* root) {
int n = root->val;
int dep = 0;
if(!root->left && !root->right) return n;
getLeft(root,n,1,dep);
return n;
}
};
广度优先BFS(O(N),O(1))
对于广度优先求解问题就比较简单了,只需要在第一次进入新的一层的时候保存此时的队首节点就是本层的最左边元素的节点值。
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
// 如果只有根节点,返回根结点的值
if(!root ->left && !root->right) return root->val;
// 层次遍历
queue<TreeNode*> que;
que.push(root);
// 初始化返回值
int res = 0;
// 访问广度遍历队列
while(!que.empty()){
size_t size = que.size();
TreeNode* node = nullptr;
// 取每一层的最左边元素
res = que.front()->val;
// 按层处理元素
while(size--){
node = que.front();
que.pop();
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
}
// 返回结果
return res;
}
};
边栏推荐
- STM32 running lantern experiment - library function version
- Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
- . DLL and Differences between lib files
- Pycharm cannot import custom package
- Crash工具基本使用及实战分享
- Liquid crystal display
- CV learning notes - clustering
- 2.Elment Ui 日期选择器 格式化问题
- 2021-01-03
- 4G module at command communication package interface designed by charging pile
猜你喜欢

JS foundation - prototype prototype chain and macro task / micro task / event mechanism

Leetcode 300 longest ascending subsequence

Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn

Dictionary tree prefix tree trie

Notes on C language learning of migrant workers majoring in electronic information engineering

Opencv notes 20 PCA

Crash工具基本使用及实战分享

Basic knowledge of communication interface

Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip

Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
随机推荐
3.2 Off-Policy Monte Carlo Methods & case study: Blackjack of off-Policy Evaluation
Leetcode 300 longest ascending subsequence
yocto 技術分享第四期:自定義增加軟件包支持
Tensorflow2.0 save model
Timer and counter of 51 single chip microcomputer
There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
STM32 running lantern experiment - library function version
Installation and removal of MySQL under Windows
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
Application of external interrupts
Screen display of charging pile design -- led driver ta6932
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
Swing transformer details-1
(2)接口中新增的方法
Stm32f04 clock configuration
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
Markdown latex full quantifier and existential quantifier (for all, existential)
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
01 business structure of imitation station B project
Google browser plug-in recommendation