Java DFS 7ms


  • 0
    S

    class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
    return getSum(root, false);
    }

    private int getSum(TreeNode node, boolean isLeft) {
        if (node==null) return 0;
        
        if (node.left==null && node.right==null) {
            return isLeft ? node.val : 0;
        }
    
        return getSum(node.left, true) + getSum(node.right, false); 
    }
    

    }


Log in to reply
 

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