slow but concise code


  • 0
    S

    the first function is responsible for start from different nodes, the second funtion is responsible for finding the number of solutions from a certain assigned node.

    int count = 0;
    public int pathSum(TreeNode root, int sum) {
        if(root == null) return 0;
        count = 0;
        find(root, sum);
        return count + pathSum(root.left, sum) + pathSum(root.right, sum);
    }
    
    public void find(TreeNode node, int val){
        if(node == null) return;
        if(node.val == val){
            count++;
        }
        find(node.left, val - node.val);
        find(node.right, val - node.val);
    }

Log in to reply
 

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