Java Recursive Solution

  • 0
        public int sumOfLeftLeaves(TreeNode root) {
            return sum(root,-1);
        public int sum(TreeNode root,int isLeft){//isLeft=1 if first parameter is a left child else 0
           if(root == null)
                return 0;
           if(root.left==null && root.right==null && isLeft == 1){
               return root.val;
           return sum(root.left,1)+sum(root.right,0);

Log in to reply

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