My Solution with a helper function


  • 0
    F
    public class Solution {
        public int sumOfLeftLeaves(TreeNode root) {
            return sum(root, false);
        }
        
        private int sum(TreeNode node, boolean isLeft) {
            if(node == null) {
                return 0;
            } else {
                if(node.left==null && node.right==null) {
                    return isLeft?node.val:0;
                } else {
                    return sum(node.left, true)+sum(node.right, false);
                }
            }
    
    }
    

    }


Log in to reply
 

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