A different recursive solution


  • 0
    I

    Just an extra string is passed.

    public class Solution {
        public int sumOfLeftLeaves(TreeNode root) {
            return sumOfLefts(root,"root");
        }
       int sumOfLefts(TreeNode node,String lor){
            if(node==null){
                return 0;
            }
            if(node.left==null&&node.right==null&& lor.equals("left")){
                return node.val;
            }
            return sumOfLefts(node.left,"left")+ sumOfLefts(node.right,"right");
           
        }
    }
    

Log in to reply
 

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