首页 > 代码库 > leetcode513 FindBottomLeftTreeValue Java

leetcode513 FindBottomLeftTreeValue Java

1、递归算法 很好理解

 1 int res = 0,h = 0;
 2 public int findBottomLeftValue(TreeNode root) {
 3     findBottomLeftValue(root, 1);
 4     return res;
 5 }
 6 public void findBottomLeftValue(TreeNode root, int depth) {
 7     if(h < depth) {res=root.val; h=depth;}
 8     if(root.left != null) findBottomLeftValue(root.left, depth+1);
 9     if(root.right !=null) findBottomLeftValue(root.right, depth+1);
10 }

 

leetcode513 FindBottomLeftTreeValue Java