LeetCode 513. 找树左下角的值 java题解
https://leetcode.cn/problems/find-bottom-left-tree-value/description/
class Solution {
int res=0;//初始值无所谓
int max_depth=0;
public int findBottomLeftValue(TreeNode root) {
find(root,0);//
return res;
}
public void find(TreeNode root,int depth){
if(root==null) return;
depth++;
if(depth>max_depth){
max_depth=depth;
res=root.val;
}
find(root.left,depth);
find(root.right,depth);
}
}
/*
左中右,遍历节点。遍历过程中记录深度。
如果他的深度>max_depth,更新为结果。
*/