Easy understand Java Solution using recursion


  • 0
    B

    public class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
    if(root == null){
    return 0;
    }
    return helper(root.left, true) + helper(root.right, 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.