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.
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 秋冬更迭秋日濃, 馬蓼花叢一株蓉。 花開百日無人嗅, 失過葉紅追跡蹤。
    耘株閱讀 183評論 0 1
  • 隆興元年,六和寺。 僵臥在床的武松正處于彌留之際。而窗外,隆冬中呼嘯的北風毫無遮攔地怒號著,似乎想把一切席卷而去。...
    小剛蛙閱讀 506評論 0 0
  • 朋友圈最濃負雞湯:來啊,互相傷害??! 1、你若盛開,蒼蠅自來。 2、這段日子迷惘又黑暗,撐過去了會有下一個黑暗等著...
    衛(wèi)財有道閱讀 493評論 0 0
  • 嗯,我承認你說的,我現(xiàn)在是迷茫還不滿現(xiàn)狀。但是,誰的青春不迷茫呢?我很羨慕那種很快就知道自己要做什么的人。我現(xiàn)在還...
    施小施閱讀 229評論 0 0

友情鏈接更多精彩內容