/** 513. 找树左下角的值
* @author 作者 Your-Name:
* @version 创建时间:2020年3月2日 下午8:58:42
* 给定一个二叉树,在树的最后一行找到最左边的值。
示例 1:
输入:
2
/ \
1 3
输出:
1
示例 2:
输入:
1
/ \
2 3
/ / \
4 5 6
/
7
输出:
7
*/
public class 找树左下角的值 {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
int maxdep=-1;
int temp=0;
public int findBottomLeftValue(TreeNode root) {
dfs(root,0);
return temp;
}
public int dfs(TreeNode root,int dep)
{
if(root==null)
return 0;
if(dep>maxdep)
{
maxdep = dep;
temp = root.val;
}
dfs(root.left,dep+1);
dfs(root.right,dep+1);
return temp;
}
}