Java recursive solution


  • 1
    E
    public class Solution {
        
        public int sumOfLeftLeaves(TreeNode root) {
            if (root == null) return 0;
            return sumHelper(root.left, true) + sumHelper(root.right, false);
        }
        
        private int sumHelper(TreeNode root, boolean isLeft) {
            if (root == null) return 0;
            if (root.left == null && root.right == null && isLeft) {
                return root.val;
            }
            return sumHelper(root.left, true) + sumHelper(root.right, false);
        }
    }

Log in to reply
 

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