二叉树的递归

1.是从根节点到叶子结点

leetcode-112

在这里插入图片描述

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def hasPathSum(self, root, sum):
        """
        :type root: TreeNode
        :type sum: int
        :rtype: bool
        """
        #边界情况1是root为None
        if not root:
            return False
            
		#边界情况2是到达叶子结点
        if not root.left and not root.right:
            return root.val == sum
            
        # or用的很好,体会一下
        return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)
leetcode-113

在这里插入图片描述

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def pathSum(self, root, sum):
        """
        :type root: TreeNode
        :type sum: int
        :rtype: List[List[int]]
        """
        #边界情况1,root为None
        if not root:
            return []
            
        #边界情况2,到达叶子结点
        if not root.left and not root.right:
        
        	#如果叶子结点值等于sum,显然需要把叶子结点加到路径中
            if root.val == sum:
                return [[sum]]
                
            #如果叶子结点值不等于sum,返回一个空路径
            else:
                return []
                
        path = [[root.val] + path for path in self.pathSum(root.left, sum - root.val)]
        path += [[root.val] + path for path in self.pathSum(root.right, sum - root.val)]
        return path
leetcode-129

在这里插入图片描述

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def sumNumbers(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        # 正是由于题目说明每个结点都存放一个0-9的数字,才可以取每个路径,然后将str转为int类型
        # 否则不可以这么做
        paths = self.getpath(root)
        res = 0
        for path in paths:
            res += int(path)
        return res

   def getpath(self, root):
       if not root:
           return []
       if not root.left and not root.right:
           return [str(root.val)]
       treepaths = [str(root.val) + path for path in self.getpath(root.left)]
       treepaths += [str(root.val) + path for path in self.getpath(root.right)]
       return treepaths
leetcode-257

在这里插入图片描述

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def binaryTreePaths(self, root):
        """
        :type root: TreeNode
        :rtype: List[str]
        """
        if not root:
            return []
        if not root.left and not root.right:
            return [str(root.val)]
        treepaths = [str(root.val) + '->' + path for path in self.binaryTreePaths(root.left)]
        treepaths += [str(root.val) + '->' + path for path in self.binaryTreePaths(root.right)]
        return treepaths

2.不需要从根节点到叶子结点,但也是一个连续的路径

leetcode-437

在这里插入图片描述

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def pathSum(self, root, sum):
        """
        :type root: TreeNode
        :type sum: int
        :rtype: int
        """
        # 在以root为根节点的二叉树中,寻找和为sum的路径,返回这样的路径个数
        if not root:
            return 0
        # 1.包含root结点,且其和为sum的路径个数
        res = self.findpath(root, sum)
        # 2.不包含root结点,且和为sum的路径个数,有两种情况,分别是左右子树
        res += self.pathSum(root.left, sum)
        res += self.pathSum(root.right, sum)
        return res

    # 正确写法
    def findpath(self, node, num):
        # 在以node为根节点的二叉树中,寻找包含node的路径,和为sum,返回这样的路径个数
        if not node:
            return 0
        res = 0
        if node.val == num:
            res += 1
        res += self.findpath(node.left, num - node.val)
        res += self.findpath(node.right, num - node.val)
        return res

    # 错误写法
    def findpath(self, node, num):
        # 在以node为根节点的二叉树中,寻找包含node的路径,和为sum,返回这样的路径个数
        if not node:
            return 0
        # 这里当node.val==num时,不应该返回1,而只是代表找到了一条路径,当前node不一定是叶子结点
        # 所以还需要继续从当前结点往下进行寻找,所以上面的写法才是正确的
        if node.val == num:
            return 1
        res = self.findpath(node.left, num - node.val)
        res += self.findpath(node.right, num - node.val)
        return res
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章