JAVA 7ms Clean and simple recursive


  • 0
    D

    '''
    public int sumOfLeftLeaves(TreeNode root) {
    return sumOfLeftLeaves(root,0);
    }

    public int sumOfLeftLeaves(TreeNode root, int s) {
        
        if(root == null)
        {
            return s;
        }
        if(root.left != null && root.left.left == null && root.left.right == null)
        {
            s += root.left.val;
        }
        if(root.left != null)
        {
            s = s+sumOfLeftLeaves(root.left);
        }
        if(root.right != null)
        {
             s = s+sumOfLeftLeaves(root.right);
        }
        return s;
    }
    

    '''


Log in to reply
 

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