LeetCode 常见二叉树问题 (python实现)

递归

树是一种常用递归操作的数据结构。一棵树要么是空,要么有两个指针,指向另外两棵树。

1. 树的高度

Leet Code 104. Maximun Depth of Binary Tree (Easy)

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxDepth(self, root: TreeNode) -> int:
        if root == None: return 0
        return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
2. 平衡树(AVL)

LeetCode 110. Balanced BInary Tree (Easy)

平衡二叉树的判断,是进一步对数的高度的计算。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def __init__(self):
        self.result = True
        
    def isBalanced(self, root: TreeNode) -> bool:
        self.maxDepth(root)
        return self.result

    def maxDepth(self, root):
        if not root: return 0
        lD = self.maxDepth(root.left)
        rD = self.maxDepth(root.right)
        if abs(lD - rD) > 1: self.result = False
        return 1 + max(lD, rD)
3. 树上两节点的最长路径

LeetCode 543. Diameter of Binary Tree (Easy)

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def __init__(self):
        self.subDepth = 0
    
    def diameterOfBinaryTree(self, root: TreeNode) -> int:
        self.calSubDepth(root)
        return self.subDepth
    
    def calSubDepth(self, root):
        if not root: return 0
        leftD = self.calSubDepth(root.left)
        rightD = self.calSubDepth(root.right)
        
        self.subDepth = max(self.subDepth, leftD + rightD)
        return max(leftD, rightD) + 1
4. 翻转树

LeetCode 226. Invert Binary Tree (Easy)

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def invertTree(self, root: TreeNode) -> TreeNode:
        if not root: return None
        left = root.left
        root.left = self.invertTree(root.right)
        root.right = self.invertTree(left)
        return root
5. 归并两棵树

LeetCode 617. Merge Two Binary Trees (Easy)

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def mergeTrees(self, t1: TreeNode, t2: TreeNode) -> TreeNode:
        if not t1: return t2
        if not t2: return t1
        if not t1 and not t2: return None
        
        t1.val += t2.val
        t1.left = self.mergeTrees(t1.left, t2.left)
        t1.right = self.mergeTrees(t1.right, t2.right)
        return t1
6. 判断路径和是否等于一个数

LeetCode 112. Path Sum (Easy)

题目中的加法,在递归过程中被转换成了减法,随着递归的进行,最后比较叶节点的值就可以判断是否存在Sum了。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def hasPathSum(self, root: TreeNode, sum: int) -> bool:
        if not root: return False
        if not root.left and not root.right and root.val == sum: return True
        return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)
7. 统计路径和为给定值的路径数量

LeetCode 437. Path Sum III

两个函数互相递归调用。

与6不同的是:6可以从根节点开始就对下一次递归进行sum减法操作。而本题,在进行下一层节点的递归时候,不可以改变sum的值。可以考虑为下一层节点为根节点。相当于遍历每一个节点的时候,都是一次问题6的操作。只是pathSumStartWithRoot()的退出条件不是叶节点,而是空节点。这里res用来记录遍历过程中的结果。

此题需要慢慢理解,两个递归函数是如何协调作用的。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def pathSum(self, root: TreeNode, sum: int) -> int:
        if not root: return 0
        res = self.pathSumStartWithRoot(root, sum) + self.pathSum(root.left, sum) + self.pathSum(root.right, sum)
        return res
        
    def pathSumStartWithRoot(self, root, sum):
        if not root: return 0
        res = 0
        if root.val == sum: res += 1
        res += self.pathSumStartWithRoot(root.left, sum - root.val) + self.pathSumStartWithRoot(root.right, sum - root.val)
        return res
8. 子树

LeetCode 572. Subtree of Another Tree

整体思路和上一题很像。区别在于上一题是数字的计算,本题是布尔值的判断

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:
        if not s: return False
        return self.sameJudge(s, t) or self.isSubtree(s.left, t) or self.isSubtree(s.right, t)
        
        
    def sameJudge(self, s, t):
        if not s and not t: return True
        if not s or not t: return False
        
        if s.val != t
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值