Simple Recursive 6 lines Java solution


  • 0
    K

    public class Solution {
    public int sumOfLeftLeaves(TreeNode root) {

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

    }


Log in to reply
 

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