class Solution {
int maxdep = -1;
int max = 0;
public int findBottomLeftValue(TreeNode root) {
dfs(root,0);
return max;
}
public void dfs(TreeNode root,int depth){
if(root == null) return;
if(root.left == null && root.right == null){
if(maxdep < depth){
maxdep = depth;
max = root.val;
}
}
if(root.left != null){
dfs(root.left, depth + 1);
}
if(root.right != null){
dfs(root.right,depth + 1);
}
}
}
这个dfs不用区分最左边节点是因为,当找到最深一层是,一定是优先找到最左节点,因为是中左右顺序遍历过去,当右边节点深度跟左边一样,是不会修改最大值的。