【leecode 树】面试刷题 树 python3实现

简单题1:二叉树前中后序遍历,递归解法和非递归解法

leecode 94. 二叉树的中序遍历
leecode 144. 二叉树的前序遍历
leecode 145. 二叉树的后序遍历

简单题2:二叉树深度,最小深度和最大深度

leecode 104. 二叉树的最大深度
leecode 111. 二叉树的最小深度

简单题3:相同的树和对称二叉树

100. 相同的树
101. 对称二叉树

简单题4:翻转二叉树

226. 翻转二叉树

简单题5:剑指 Offer 54. 二叉搜索树的第k大节点

解法:二叉搜索树的中序遍历是递增排序,遍历方式为左根右,将遍历顺序改为右根左,即为降序排列,取第k个值即可。

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

class Solution:
    def kthLargest(self, root: TreeNode, k: int) -> int:
        self.cnt = 0
        def dfs(root_n):
            if not root_n:
                return

            dfs(root_n.right)            
            self.cnt += 1
            if self.cnt == k:
                self.re = root_n.val
            dfs(root_n.left)
        dfs(root)
        return self.re

简单题6:公共祖先
leecode 235、leecode 236 和 leecode 1644: 二叉树的最近公共祖先

235:给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。

方法:

  • 从根节点开始遍历
  • 节点p和q的值都大于节点root的值,那么这两个点一定在root的右边
  • 节点p和q的值都小于节点root的值,那么这两个点一定在root的左边
  • 如果出现分岔节点,则该节点就是最近的公共祖先
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        cur = root
        while True:
            if cur.val < p.val and cur.val < q.val:
                cur = cur.right
            elif cur.val > p.val and cur.val > q.val:
                cur = cur.left
            else:
                break
        return cur    

236:给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。(两个节点都存在

1644:给定一棵二叉树的根节点 root,返回给定节点 p 和 q 的最近公共祖先(LCA)节点。
如果 p 或 q 之一不存在于该二叉树中,返回 null。
树中的每个节点值都是互不相同的。

236解法:

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

class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        if not root:
            return None
        
        if root == p or root == q:
            return root
        
        left = self.lowestCommonAncestor(root.left, p, q)
        right = self.lowestCommonAncestor(root.right, p, q)

        if left and right:
            return root
      
       return left if left else right

1644解法:1644的解法适用于236,此题使用了全局变量记录两个节点是否存在

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

class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        global findp
        global findq
        findp, findq = False, False
        ans = self.dfs(root, p, q)
        if findp and findq:
            return ans
        return None         
        
    def dfs(self, root, p, q):
        global findp
        global findq
        if not root:
            return root
        
        if root == p:
            findp = True
        elif root == q:
            findq = True
        
        left = self.dfs(root.left, p, q)
        right = self.dfs(root.right, p, q)

        if root == p or root == q or (left and right):
            return root
        
        return left if left else right 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值