Leetcode513. Find Bottom Left Tree Value

Given a binary tree, find the leftmost value in the last row of the tree


Basic idea:

I use two global variables to store current level value and current qualified answer.
Here are my naive solution.

class Solution{
    private int max = 0;//to store the current max level val.
    private int res = 0;// to store the current qualified val.
    public int findBottomLeftValue(TreeNode root) {
          helper(root,1);
          return res;
     }  
 public void helper(TreeNode node, int level){
        if(node == null){//base case 
           return ;
        }
        if(max < level){
            max = level;
            res = node.val;//update the max level value and res.
        }
         if (node.left!=null) helper(node.left,level+1);
         if (node.right!=null) helper(node.right,level+1);//left and righe node are in the same level 
         //if I use "level++"here, then when it comes to the right node, the level value has been increased,
        // thus, the right node level value is more than the left one, which is incorrect.
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 秋冬更迭秋日浓, 马蓼花丛一株蓉。 花开百日无人嗅, 失过叶红追迹踪。
    耘株阅读 168评论 0 1
  • 隆兴元年,六和寺。 僵卧在床的武松正处于弥留之际。而窗外,隆冬中呼啸的北风毫无遮拦地怒号着,似乎想把一切席卷而去。...
    小刚蛙阅读 469评论 0 0
  • 朋友圈最浓负鸡汤:来啊,互相伤害啊! 1、你若盛开,苍蝇自来。 2、这段日子迷惘又黑暗,撑过去了会有下一个黑暗等着...
    卫财有道阅读 462评论 0 0
  • 嗯,我承认你说的,我现在是迷茫还不满现状。但是,谁的青春不迷茫呢?我很羡慕那种很快就知道自己要做什么的人。我现在还...
    施小施阅读 213评论 0 0