Solved by recursively traversing the given binary tree and maintaining “level” which will store the current node’s level in the tree.


  • 0
    N
    
    class Solution {
    public:
        int findBottomLeftValue(TreeNode* root) 
        {
              if (! root) return  0 ;
             int max_depth = 1 , res = root-> val;
            Helper (root, 1 , max_depth, res);
             return res;
        }
         void Helper (TreeNode * node, int depth, int & max_depth, int & res) 
         {
             if (! node) return ;
             if (depth> max_depth) 
             {
                max_depth = depth;
                res = node-> val;
            }
    
            Helper (node -> left, depth + 1 , max_depth, res);
            Helper (node -> right, depth + 1 , max_depth, res);
        }
    };
    

    If current node is left leaf, then check if its level is more than the level of deepest left leaf seen so far.
    -> If level is more then update the result.
    If current node is not leaf, then recursively find maximum depth in left and right subtrees, and return maximum of the two depths.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.