题目来源
题目描述
题目解析
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private int maxDepth = -1;
private int ans;
public int findBottomLeftValue(TreeNode root) {
helper(root, 0);
return ans;
}
private void helper(TreeNode root, int depth){
if (root.left == null && root.right == null){
if (depth > maxDepth){
maxDepth = depth;
ans = root.val;
}
return;
}
if (root.left != null){
helper(root.left, depth + 1);
}
if (root.right != null){
helper(root.right, depth + 1);
}
}
}