首页 > 试题广场 >

二叉树中和为某一值的路径(三)

[编程题]二叉树中和为某一值的路径(三)
  • 热度指数:28061 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个二叉树root和一个整数值 sum ,求该树有多少路径的的节点值之和等于 sum 。
1.该题路径定义不需要从根节点开始,也不需要在叶子节点结束,但是一定是从父亲节点往下到孩子节点
2.总节点数目为n
3.保证最后返回的路径个数在整形范围内(即路径个数小于231-1)

数据范围:



假如二叉树root为{1,2,3,4,5,4,3,#,#,-1},sum=6,那么总共如下所示,有3条路径符合要求

示例1

输入

{1,2,3,4,5,4,3,#,#,-1},6

输出

3

说明

如图所示,有3条路径符合      
示例2

输入

{0,1},1

输出

2
示例3

输入

{1,#,2,#,3},3

输出

2

说明:本题目包含复杂数据结构TreeNode,点此查看相关信息
class Solution:
    # 两层递归
    # 时间复杂度:O(n2)  空间复杂度:O(n)
    n = 0
    def recursion(self, root, sum_):
        if not root:
            return 
        if sum_ == root.val:
            self.n += 1 
        self.recursion(root.left, sum_- root.val)  
        self.recursion(root.right, sum_- root.val)  

    def FindPath(self , root: TreeNode, sum_: int) -> int:
        if not root:
            return self.n
        self.recursion(root, sum_)
        # 以其子节点为新根
        self.FindPath(root.left, sum_)
        self.FindPath(root.right, sum_)
        return self.n
发表于 2022-05-13 13:02:47 回复(0)
class Solution:
    def __init__(self):
        self.mp = {}
    
    def dfs(self, root, sum, lsum):
        if not root:
            return 0
        res = 0
        tmp = root.val + lsum
        if self.mp.__contains__(tmp - sum):
            res += self.mp[tmp - sum]
        if tmp in self.mp:
            self.mp[tmp] += 1
        else:
            self.mp[tmp] = 1
        res += self.dfs(root.left, sum, tmp)
        res += self.dfs(root.right, sum, tmp)
        self.mp[tmp] -= 1
        return res
        
    def FindPath(self , root: TreeNode, sum: int) -> int:
        # write code here
        self.mp[0] = 1
        return self.dfs(root, sum, 0)
python实现的一次dfs+哈希表  时间复杂度o(n) 空间复杂度o(n)
发表于 2022-04-22 22:02:11 回复(0)
class Solution:
    def __init__(self):
        self.count = 0
    def FindPath(self , root: TreeNode, sum: int) -> int:
        hashmaps = {}
        hashmaps[0] = 1
        def getNums(root, prefix):
            if not root:
                return
            prefix = prefix + root.val
            if prefix - sum in hashmaps:
                self.count += hashmaps[prefix - sum]
            if prefix in hashmaps:
                hashmaps[prefix] += 1
            else:
                hashmaps[prefix] = 1
            getNums(root.left, prefix)
            getNums(root.right, prefix)
        getNums(root, 0)
        return self.count
前缀和+哈希
发表于 2022-04-16 22:05:54 回复(0)
class Solution:
    def FindPath(self , root: TreeNode, sum: int) -> int:
        # write code here
        if not root:
            return 0
        def find(root,target):
            if not root:
                return 0
            all_res=0
            if root.val==target:
                all_res=1
            return find(root.left,target-root.val)+find(root.right,target-root.val)+all_res
        return find(root,sum)+self.FindPath(root.left, sum)+self.FindPath(root.right, sum)

            

发表于 2022-01-27 08:25:00 回复(0)
python代码。思路:定义求每个节点的路径方法,然后遍历每个节点
class Solution:
    def __init__(self):
        self.count = 0
        
    def FindPath(self , root: TreeNode, sum: int) -> int:
        self.rec(root,sum)
        return self.count
    
    def find(self,root,sum):
        if not root:return
        sum -= root.val
        if sum == 0:self.count += 1
        self.find(root.left,sum)
        self.find(root.right,sum)
    
    def rec(self,root,sum):
        if not root:return
        self.find(root,sum)
        self.rec(root.left,sum)
        self.rec(root.right,sum)


发表于 2022-01-17 12:26:21 回复(0)
class Solution:
    def FindPath(self , root: TreeNode, sum: int) -> int:
        # write code here
        num_path = 0
        def dfs(node, res_list):
            nonlocal num_path
            if not node:
                return
            cur_res_list = [0]
            for sum_val in res_list:
                cur_sum = sum_val + node.val
                cur_res_list.append(cur_sum)
                if cur_sum == sum:
                    num_path += 1
            dfs(node.left, cur_res_list)
            dfs(node.right, cur_res_list)
            return
        dfs(root, [0])
        return num_path
每个节点记录当前节点可能的路径和[0, 当前node val, 当前node val+ 父节点val, 当前node val+ 父节点val + 父父节点val ...]
一次dfs即可

发表于 2022-01-09 18:12:26 回复(0)
class Solution:
    def __init__(self):
        self.res = 0
        
    def CountPath(self , root: TreeNode, now: int) -> int:
        if not root:
            return 0
        if root.val == now:
            return 1 + self.CountPath(root.left, now - root.val) + self.CountPath(root.right, now - root.val)
        else:
            return self.CountPath(root.left, now - root.val) + self.CountPath(root.right, now - root.val)

    def FindPath(self , root: TreeNode, sum: int) -> int:
        # write code here
        if not root:
            return 0
        self.res += self.CountPath(root, sum)
        self.FindPath(root.left, sum)
        self.FindPath(root.right, sum)
        
        return self.res

发表于 2021-12-01 21:45:16 回复(0)
class Solution:
    # 以root为起始节点可得到的路径数
    def getCount(self, root, count, sum):
        if not root:
            return
        # 这里必须先减去root的值,否则只有一个节点时会出问题
        if sum - root.val == 0:
            count[0] += 1
        self.getCount(root.left, count, sum - root.val)
        self.getCount(root.right, count, sum - root.val)

    def FindPath(self, root: TreeNode, sum: int) -> int:
        if not root:
            return 0
        count = [0]
        self.getCount(root, count, sum)
        res = count[0] + self.FindPath(root.left, sum) + self.FindPath(root.right, sum)
        return res

发表于 2021-11-22 14:51:46 回复(0)