Java Clean Recursion Solution


  • 0
    W

    Using one parameter boolean isLeft to check whether this leaf is on the left side.

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

Log in to reply
 

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