当前位置:网站首页>Li Kou daily question leetcode 513. find the value in the lower left corner of the tree
Li Kou daily question leetcode 513. find the value in the lower left corner of the tree
2022-07-27 06:21:00 【The last tripod】
leetcode 513. Find the value in the lower left corner of the tree 

Ideas Simple depth traversal , It combines the idea of post order traversal of binary tree . First, keep walking down , When traversing downward, first traverse left ( Ensure that the deepest value obtained is on the far left ), If you find that the traversal depth is greater than the existing depth, change the maximum depth to the current depth , Values are also modified .
class Solution {
int val;
int deep = -1;
public void dfs(TreeNode root,int depth){
if(root == null)return;
dfs(root.left,depth+1);// Traverse left
dfs(root.right,depth+1);// Traverse right
if(depth > deep){
// When the code runs here, all the child nodes of this node have been traversed , If you don't understand, you can look at the post order traversal of binary trees , The core is backtracking
deep = depth;// Modify depth
val = root.val;// Modified value
}
}
public int findBottomLeftValue(TreeNode root) {
dfs(root,0);
return val;
}
}
边栏推荐
猜你喜欢
随机推荐
Non photorealistic rendering (NPR) paper understanding and reproduction (unity) - stylized highlights for cartoon rendering and animation
shell script if嵌套for循环脚本
Automated Deployment Project
yum获取rpm软件包的三种方法
[Arduino] reborn Arduino monk (1)
Remote sensing image recognition - making data sets
UnityShader-高斯模糊
Dynamic planning for solving problems (5)
文件内容的读写——数据流
ROS workspace coverage
通信机制案例
Calculation of Huffman tree, code implementation and proof, graphic interpretation
Tangent space and TBN matrix
Unity engine starts to migrate from mono to.Net coreclr
Strategies for common locks in multithreading
力扣题解 动态规划(2)
内部类的相关知识
论文报告-Linear Regression for face recognition
IP核之PLL
如何选择正确的服务器备份方法









