Sum of Left Leaves - Java short recursive solution


  • 0
    D
    public class Solution {
        private int sumLeft( TreeNode root, boolean addMe ) {
            if( root == null )  return 0;
            if( root.left == null && root.right == null && addMe )   return root.val; //add my value if I'm left leaf
            return sumLeft( root.left, true ) + sumLeft( root.right, false );
        }
        
        public int sumOfLeftLeaves(TreeNode root) {
            return sumLeft( root, false );
        }
    }
    

    Runtime: O(n)


Log in to reply
 

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