Java dfs O(1) space


  • 0
    U
    int res = -1;
    int maxDepthSoFar = -1;
    
    public int findBottomLeftValue(TreeNode root) {
        if (root == null) {
            return -1;
        }
        helper(root, 0);
        return res;
    }
    
    private void helper(TreeNode root, int depth) {
        if (root == null) {
            return;
        }
        if (depth > maxDepthSoFar) {
            res = root.val;
            maxDepthSoFar = depth;
        }
        helper(root.left, depth + 1);
        helper(root.right, depth + 1);
    }

Log in to reply
 

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