Skip to content

437 路径总和 III

code

javascript
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} targetSum
 * @return {number}
 */
var pathSum = function(root, targetSum) {
    let count = 0;
    function traverse(root, target){
        console.log('本次root:' + root);
        console.log('本次剩余' + target);
        if(root === null){
            return;
        }
        if(target - root.val === 0){
            count++;
            console.log('看看怎么个事');
            console.log(root.val);
            traverse(root.left, targetSum);
            traverse(root.right, targetSum);
        }
        
            traverse(root.left, targetSum);
            traverse(root.right, targetSum);
            traverse(root.left, (target - root.val));
            traverse(root.right, (target - root.val));
        
    }
    traverse(root, targetSum);
    return count;
};

总结