Easy to understand Java DFS Solution


  • 0
    H

    Quite straight forward solution. Update max level when you reached a level deeper than global maxLevel. Update the result value only when you first reached to this level, otherwise skip.
    ···
    public class Solution {
    int maxLevel = 1;
    int result;

    public int findBottomLeftValue(TreeNode root) 
    {
        result = root.val;
        findTreeNode(root, 1);
        return result;
    }
    
    private void findTreeNode(TreeNode node, int level)
    {
        if(node == null)
            return;
            
        if(level > maxLevel)
        {
            result = node.val;
            maxLevel = level;
        }
        
        findTreeNode(node.left, level + 1);
        findTreeNode(node.right, level + 1);
    }
    

    }
    ···


Log in to reply
 

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