当前位置:网站首页>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;
}
};
边栏推荐
- QT detection card reader analog keyboard input
- (1) What is a lambda expression
- Swing transformer details-2
- Gpiof6, 7, 8 configuration
- Markdown latex full quantifier and existential quantifier (for all, existential)
- El table X-axis direction (horizontal) scroll bar slides to the right by default
- 2021-01-03
- Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
- CV learning notes - scale invariant feature transformation (SIFT)
- RESNET code details
猜你喜欢

openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍

Opencv Harris corner detection

Gpiof6, 7, 8 configuration

CV learning notes - edge extraction

yocto 技術分享第四期:自定義增加軟件包支持

Connect Alibaba cloud servers in the form of key pairs

RESNET code details

LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)

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

There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
随机推荐
Opencv note 21 frequency domain filtering
Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
Opencv interview guide
Blue Bridge Cup for migrant workers majoring in electronic information engineering
Opencv feature extraction - hog
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
Tensorflow built-in evaluation
01仿B站项目业务架构
Installation and removal of MySQL under Windows
LeetCode - 673. Number of longest increasing subsequences
Dictionary tree prefix tree trie
LeetCode - 715. Range 模块(TreeSet) *****
Opencv notes 20 PCA
The data read by pandas is saved to the MySQL database
Tensorflow2.0 save model
03 FastJson 解决循环引用
3.2 Off-Policy Monte Carlo Methods & case study: Blackjack of off-Policy Evaluation
Retinaface: single stage dense face localization in the wild
Development of intelligent charging pile (I): overview of the overall design of the system
Application of external interrupts