Simple recursive Java solution that beats 73%


  • 0
    P
    public class Solution {
        public int sumOfLeftLeaves(TreeNode root) {
            if (root == null) return 0;
            if (root.left == null) return sumOfLeftLeaves(root.right);
            if (root.left.left == null && root.left.right == null) return sumOfLeftLeaves(root.right) + root.left.val;
            return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
        }
    }
    

Log in to reply
 

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