17. 树 - 统计路径和等于一个数的路径数量

1.题目

LeetCode: 437. 路径总和 III

【medium】

给定一个二叉树,它的每个结点都存放着一个整数值。

找出路径和等于给定数值的路径总数。

路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)。

二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数。

示例:

root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8

      10
     /  \
    5   -3
   / \    \
  3   2   11
 / \   \
3  -2   1

返回 3。和等于 8 的路径有:

1.  5 -> 3
2.  5 -> 2 -> 1
3.  -3 -> 11

2.解题

方法一:递归法

递归出口:if (root == null) return 0;

题目要求路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点),所以,有三种情况:

(1)以当前节点为开始节点计算路径和

(2)以当前节点的左孩子为开始节点计算路径和

(3)以当前节点的右孩子为开始节点计算路径和

java:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int pathSum(TreeNode root, int sum) {
        if (root == null) return 0;
        return pathSum2(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
    }

    public int pathSum2(TreeNode root, int sum) {
        if (root == null) return 0;
        int res = 0;
        if (root.val == sum) res++;
        res += pathSum2(root.left, sum - root.val) + pathSum2(root.right, sum - root.val);
        return res;
    }
}

时间复杂度: O ( n 2 ) O(n^2) O(n2) n + ( n − 1 ) + ( n − 2 ) + . . . + 1 = n ( n + 1 ) / 2 ≈ n 2 n + (n - 1) + (n - 2) + ... + 1 = n(n + 1) / 2 ≈ n^2 n+(n1)+(n2)+...+1=n(n+1)/2n2,由于要以每个节点为开始节点进行递归求路径和,所以结果是一个等差数列的求和

空间复杂度: O ( n 2 ) O(n^2) O(n2)

方法二:递归+队列

计算路径和时用队列,然后递归求解左子树和右子树

java:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int pathSum(TreeNode root, int sum) {
        if (root == null) return 0;
        int res = 0;
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        Queue<Integer> val = new LinkedList<Integer>();
        queue.offer(root);
        val.offer(root.val);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            int num = val.poll();
            if (num == sum) res++;
            if (node.left != null) {
                queue.offer(node.left);
                val.offer(node.left.val + num);
            }
            if (node.right != null) {
                queue.offer(node.right);
                val.offer(node.right.val + num);
            }
        }
        res += pathSum(root.left, sum) + pathSum(root.right, sum);
        return res;
    }
}

时间复杂度: O ( n 2 ) O(n^2) O(n2)

空间复杂度: O ( n 2 ) O(n^2) O(n2)

方法三:精选题解-前缀和

参考:

https://leetcode-cn.com/problems/path-sum-iii/solution/qian-zhui-he-di-gui-hui-su-by-shi-huo-de-xia-tian/

java:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int pathSum(TreeNode root, int sum) {
        Map<Integer, Integer> preSumCount = new HashMap<>();
        preSumCount.put(0, 1);  # 加入初始值
        return pathSum2(root, preSumCount, sum, 0);
    }

    public int pathSum2 (TreeNode root, Map<Integer, Integer> preSumCount, int sum, int curSum) {
        if (root == null) return 0;
        int res = 0;
        curSum += root.val;
        res += preSumCount.getOrDefault(curSum - sum, 0);
        preSumCount.put(curSum, preSumCount.getOrDefault(curSum, 0) + 1);
        // 递归计算左子树和右子树
        res += pathSum2(root.left, preSumCount, sum, curSum) + pathSum2(root.right, preSumCount, sum, curSum);
        preSumCount.put(curSum, preSumCount.getOrDefault(curSum, 0) - 1);
        return res;
    }
}

时间复杂度:O(n)

空间复杂度:O(n)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值