My JS Preorder Solution


  • 0
    A
    var res;
    var pathSum = function(root, sum) {
        res=0;
        pathSumHelper(root, sum);
        return res;
        
    };
    
    var pathSumHelper = function(root, sum) {
        if(root==null) return 0;
        dfs(root,sum);
        if(root.left)
            pathSumHelper(root.left,sum);
        if(root.right)
            pathSumHelper(root.right,sum);
        return res;
        
    };
    
    var dfs=function(node,sum){
        if(node==null) return;
        if(node.val==sum) res++;
        dfs(node.left, sum-node.val);
        dfs(node.right, sum-node.val);
        
    };
    

Log in to reply
 

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