8ms Java Recursive Solution


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

Log in to reply
 

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