首页 > 试题广场 >

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

[编程题]二叉树中和为某一值的路径(一)
  • 热度指数:163461 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个二叉树root和一个值 sum ,判断是否有从根节点到叶子节点的节点值之和等于 sum 的路径。
1.该题路径定义为从树的根结点开始往下一直到叶子结点所经过的结点
2.叶子节点是指没有子节点的节点
3.路径只能从父节点到子节点,不能从子节点到父节点
4.总节点数目为n

例如:
给出如下的二叉树,

返回true,因为存在一条路径 的节点值之和为 22

数据范围:
1.树上的节点数满足
2.每 个节点的值都满足
要求:空间复杂度 ,时间复杂度
进阶:空间复杂度 ,时间复杂度
示例1

输入

{5,4,8,1,11,#,9,#,#,2,7},22

输出

true
示例2

输入

{1,2},0

输出

false
示例3

输入

{1,2},3

输出

true
示例4

输入

{},0

输出

false

说明:本题目包含复杂数据结构TreeNode,点此查看相关信息
class Solution:
    result = False
    def hasPathSum(self , root: TreeNode, sum: int) -> bool:
        # write code here
        if root == None:
            return False
        def dfs(root, num):
            if root.left == None and root.right == None:
                num += root.val
                if num == sum:
                   
                    self.result = True
                return
            num += root.val
            if root.left != None:
                dfs(root.left, num)
            if root.right != None:
                dfs(root.right, num)
       
        dfs(root, 0)
        return self.result
发表于 2023-08-09 13:57:54 回复(0)
class Solution:
    def hasPathSum(self , root: TreeNode, sum: int) -> bool:
        # write code here
        if root:
            sum=sum-root.val
            if sum==0 and not root.left and not root.right:
                return True
            else:
               return self.hasPathSum(root.left,sum)&nbs***bsp;self.hasPathSum(root.right,sum)
        if not root:
            return False

发表于 2023-03-02 19:21:28 回复(0)
class Solution:
    def hasPathSum(self , root: TreeNode, sum: int) -> bool:
        if root is None:
            return False
        if root.left is None and root.right is None:
            return root.val == sum
        left = self.hasPathSum(root.left, sum - root.val) if root.left else False
        right = self.hasPathSum(root.right, sum - root.val) if root.right else False
        return left&nbs***bsp;right

发表于 2023-03-01 10:06:25 回复(0)
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param root TreeNode类 
# @param sum int整型 
# @return bool布尔型
#
class Solution:
    def hasPathSum(self , root: TreeNode, sum: int) -> bool:
        # write code here
        if root and not root.left and not root.right and sum-root.val==0:
        #是叶子,并且sum-root.val==0不为0,注意此处不能为sum==0
            return True
        if not root:#是叶子,并且sum不为0
            return False
        return self.hasPathSum(root.left,sum-root.val)&nbs***bsp;self.hasPathSum(root.right,sum-root.val)

发表于 2022-08-19 13:33:48 回复(1)
# 给定一个二叉树root和一个值 sum ,判断是否有从根节点到叶子节点的节点值之和等于 sum 的路径。

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

#回溯法
class Solution:
    def hasPathSum(self , root: TreeNode, sum: int) -> bool:
        # write code her
        sums=[]
        def pre_order(root,sn):
            if not root:
                return 
            # 开始回溯
            sn+=root.val
            # 如果来到叶子节点,就找到了一条路径,将此时的 路径组成值 添加到结果列表sums中
            if not root.left and not root.right and sn==sum:
                sums.append(sn)

            pre_order(root.left,sn)
            pre_order(root.right,sn)
            
            # 恢复回溯前的状态
            sn-=root.val

        sn=0
        pre_order(root,sn)
        print(sums)
        for i in sums:
            if i==sum:
                return True
        return False

# 回溯法:不记录每一条路径
class Solution:
    def hasPathSum(self , root: TreeNode, sum: int) -> bool:
        # write code her
        def pre_order(root,sn):
            if not root:
                return False
            # 开始回溯
            sn+=root.val
            # 来到叶子节点
            if not root.left and not root.right and sn==sum:
                return True

            l_find=pre_order(root.left,sn)
            r_find=pre_order(root.right,sn)
            
            # 恢复回溯前的状态
            sn-=root.val
            return l_find&nbs***bsp;r_find

        sn=0
        return pre_order(root,sn)

# 直接递归:直接把sn-=root.val 删掉就是了,这个函数后面都没再用到sn
class Solution:
    def hasPathSum(self , root: TreeNode, sum: int) -> bool:
        # write code her
        def pre_order(root,sn):
            if not root:
                return False
            sn+=root.val
            # 来到叶子节点
            if not root.left and not root.right and sn==sum:
                return True

            l_find=pre_order(root.left,sn)
            r_find=pre_order(root.right,sn)
            return l_find&nbs***bsp;r_find
        sn=0
        return pre_order(root, sn)

# 直接递归更直接的是这个写法
class Solution:
    def hasPathSum(self , root: TreeNode, sum: int) -> bool:
        # write code her
        if not root:
            return False
        sum-=root.val
        # 来到叶子节点
        if not root.left and not root.right and sum==0:
            return True

        l_find=self.hasPathSum(root.left,sum)
        r_find=self.hasPathSum(root.right,sum)
        return l_find&nbs***bsp;r_find
# 或者这样
class Solution:
    def hasPathSum(self , root: TreeNode, sum: int) -> bool:
        # write code her
        if not root:
            return False
        
        # 来到叶子节点
        if not root.left and not root.right and sum-root.val==0:
            return True

        l_find=self.hasPathSum(root.left,sum-root.val)
        r_find=self.hasPathSum(root.right,sum-root.val)
        return l_find&nbs***bsp;r_find

发表于 2022-02-24 22:01:38 回复(0)
class Solution:
    def hasPathSum(self , root: TreeNode, sum: int) -> bool:
        # write code here
        if root == None:
            return False
        sum = sum - root.val
        if sum == 0 and root.left == None and root.right == None:
            return True
        root_left = self.hasPathSum(root.left, sum)
        root_right = self.hasPathSum(root.right, sum)
        return root_left or root_right
发表于 2021-11-26 19:40:14 回复(0)

问题信息

难度:
8条回答 28899浏览

热门推荐

通过挑战的用户