simple Java recursive DFS solution.


  • 0
    J
        public int sumOfLeftLeaves(TreeNode root) {
            if (root == null || root.left == null && root.right == null) return 0; 
            return sum(root.left, true) + sum(root.right, false);
        }
        
        private int sum(TreeNode root, boolean isLeft) {
            if (root == null) return 0;
            if (root.left == null && root.right == null && isLeft) return root.val;
            return sum(root.left, true) + sum(root.right, false);
        }
    

Log in to reply
 

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