Java recursive with side information

  • 0

    Have a variable to indicate the side. At every node, check the following,
    if you are a left leaf, (with both children as null and side as 1), then return ur value
    else return the sum of values returned by recursive left and right call

        public int sumOfLeftLeaves(TreeNode root) {
            return helper(root,-1);
        public int helper(TreeNode root, int side){
            if(root == null)return 0;
            if( (root.left == null) && (root.right == null) && side == 1 ){
                return root.val;
             return (helper(root.left,1)+ helper(root.right,0));   

Log in to reply

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