LeetCode-513. 找树左下角的值-深度优先搜索

/** 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;
	}
}
发布了72 篇原创文章 · 获赞 7 · 访问量 4100

猜你喜欢

转载自blog.csdn.net/l769440473/article/details/104670126