Very elegant solution in Java (5 lines)

  • 0
        public int sumOfLeftLeaves(TreeNode root) {
            return sumOfLeftLeaves(root, false);
        public int sumOfLeftLeaves(TreeNode root, boolean isLeft) {
            if (root == null) return 0;
            if (root.left == null && root.right == null && isLeft) return root.val;
            return sumOfLeftLeaves(root.left, true) + sumOfLeftLeaves(root.right, false);

Log in to reply

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