Simple Java Recursive Solution


  • 0
    R
    public class Solution {
        int sum = 0;
        public int sumOfLeftLeaves(TreeNode root) {
            if( root == null || isLeaf( root ))  return 0;
            
            if( isLeaf( root.left) )   sum += root.left.val;
            
            sumOfLeftLeaves( root.left );
            sumOfLeftLeaves( root.right );
            
            return sum;
        }
        
        public boolean isLeaf( TreeNode root ){
            if( root == null )  return false;
            return root.left == null && root.right == null;
        }
    }
    

Log in to reply
 

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