Java recursive solution 8ms runtime


  • 0
    Y
    public int sumOfLeftLeaves(TreeNode root) {
            if (root == null) {
            	return 0;
            } else {
                if (root.left != null)
                    if (root.left.left == null && root.left.right == null)
            	        return root.left.val + sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
                    else
                        return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
                else
                    return sumOfLeftLeaves(root.right);
            }
        }
    

Log in to reply
 

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