【力扣十道】二叉树

刷题来劲了啊,让我们先膜一下神,树+递归一直是我的害怕点,因为python更擅长字符串又没有指针,但是看完之后确实没那么恐惧了,就算不能去,也是很快乐的,刷完20道然后复习一下操作系统我就要去“地狱”了

在这里插入图片描述

一.递归模版

1.1 一个函数搞定

450.删除二叉搜索树中的节点

class Solution:
    def deleteNode(self,root,key):
        self.pre = 0
        def travel(root):
            if not root:
                return 

            root.left = travel(root.left)
            print(root.val)
            if root.val == key:
                if not root.left and not root.right:
                    return None
                elif not root.left: 
                    return root.right
                elif not root.right: 
                    return root.left
                else:
                    
                    root.val = self.pre
                    root.left = self.deleteNode(root.left,self.pre)
                    return root
            self.pre = root.val
            root.right = travel(root.right)
            return root
        root = travel(root)
        return root

700. 二叉搜索树中的搜索

class Solution:
    def searchBST(self, root: TreeNode, val: int) -> TreeNode:
        if not root:
            return None
        elif root.val == val:
            return root
        else:
            return  self.searchBST(root.left, val) or self.searchBST(root.right,val)

利用二叉树特性

class Solution:
    def searchBST(self, root: TreeNode, val: int) -> TreeNode:
        if not root: return None
        if root.val == val: return root
        elif root.val > val: return self.searchBST(root.left,val)
        elif root.val < val: return self.searchBST(root.right,val)
        else: return None

701. 中等-二叉搜索树中的插入操作

class Solution:
    def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
        if not root:
            return TreeNode(val)
        if val > root.val :
            root.right = self.insertIntoBST(root.right,val)
        else:
            root.left =  self.insertIntoBST(root.left,val)
        return root

100. 简单-相同的树

class Solution:
    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        if not p  and not q :
            return True
        elif not p  or not q :
            return False
        elif p.val != q.val:
            return False
        return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)

剑指 Offer 07. 重建二叉树
在这里插入图片描述

剑指 Offer 55 - I. 二叉树的深度

class Solution:
    def maxDepth(self, root: TreeNode) -> int:
        if not root:
            return 0
        return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1

利用上题解下题
剑指 Offer 55 - II. 平衡二叉树

class Solution:
    def isBalanced(self, root: TreeNode) -> bool:
        def TreeHight(root):
            if not root:
                return 0
            return max(TreeHight(root.left),TreeHight(root.right))+1
        if not root:
            return True
        leftTreeHight = TreeHight(root.left)
        rightTreeHight = TreeHight(root.right)
        if abs(leftTreeHight - rightTreeHight)>1:
            return False
        else:
            return self.isBalanced(root.left) and self.isBalanced(root.right)
        

上面的方法虽然可行高端,但是下面的更容易想到


class Solution:
    def isBalanced(self, root: TreeNode) -> bool:
        self.ans = True
        def travel(root):
            if not root:
                return 0
            left = travel(root.left)
            right = travel(root.right)
            if abs(left - right) > 1: self.ans = False
            return max(left,right) + 1
        travel(root)
        return self.ans

222. 完全二叉树的节点个数

class Solution:
    def countNodes(self, root: TreeNode) -> int:
        if not root: return 0
        return self.countNodes(root.left) + self.countNodes(root.right) +1

剑指 Offer 27. 二叉树的镜像

class Solution:
    def mirrorTree(self, root: TreeNode) -> TreeNode:
        if not root:
            return root
        root.left,root.right = root.right,root.left
        self.mirrorTree(root.left)
        self.mirrorTree(root.right)
        return root

剑指 Offer 68 - I. 二叉搜索树的最近公共祖先

class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        if root.val > p.val and root.val > q.val : return self.lowestCommonAncestor(root.left , p , q)
        if root.val < p.val and root.val < q.val : return self.lowestCommonAncestor(root.right , p , q)
        return root

剑指 Offer 68 - II. 二叉树的最近公共祖先

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

1.2 需要两个函数

98. 验证二叉搜索树

class Solution:
    def isValidBST1(self,root: TreeNode,lower = float('-inf'), upper = float('inf')):

        if not root:
            return True
        elif root.val <= lower or root.val >= upper:
            return False
        return self.isValidBST1(root.left,lower,root.val) and self.isValidBST1(root.right,root.val,upper)


    def isValidBST(self, root: TreeNode) -> bool:
        return self.isValidBST1(root)

剑指 Offer II 052. 展平二叉搜索树

class Solution:

    def increasingBST(self, root: TreeNode) -> TreeNode:
        self.pre = ans = TreeNode(0)
        def dfs(root):
            if not root : return
            dfs(root.left)
            root.left = None
            self.pre.right = root
            self.pre = root
            dfs(root.right)
        dfs(root)
        return ans.right

863. 二叉树中所有距离为 K 的结点

比较难,哈希父节点,然后三个方向走

class Solution:
    def distanceK(self, root: TreeNode, target: TreeNode, k: int) -> List[int]:
        self.parent = defaultdict(int)
        def findParents(root):
            if root.left:
                self.parent[root.left.val] = root
                findParents(root.left)
            if root.right:
                self.parent[root.right.val] = root
                findParents(root.right)
        self.ans =[]
        findParents(root)
        def findAns(root,orig,depth,k):
            if not root:
                return
            if depth == k:
                self.ans.append(root.val)
                return
            if root.left != orig:
                findAns(root.left ,root, depth + 1 , k )
            if root.right != orig:
                findAns(root.right, root,depth + 1, k )
            if self.parent[root.val] != orig:
                findAns(self.parent[root.val], root,depth + 1, k)
        findAns(target,target,0,k)
        return self.ans

中序遍历

98. 验证二叉搜索树
在这里插入图片描述

中序遍历思路一

class Solution:
    pre = None
    def isValidBST(self, root: TreeNode) -> bool:
        if not root:
            return True
        left = self.isValidBST(root.left)
        if self.pre and self.pre.val >= root.val:
            return False
        self.pre = root

        right = self.isValidBST(root.right)
        return left and right

中序遍历思路二 存成数组

class Solution:
    def isValidBST(self, root: TreeNode) -> bool:
        # 中序遍历,递归
        def tree(node):
            if not node:
                return
            tree(node.left)
            ls.append(node.val)
            tree(node.right)
        ls = []
        tree(root)
        for i in range(len(ls)-1):
            if ls[i] >= ls[i+1]:
                return False
        return True

作者:huoming
链接:https://leetcode-cn.com/problems/validate-binary-search-tree/solution/zhong-xu-bian-li-di-gui-by-huoming-jku4/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

二.层序遍历模版

class Solution:
    def levelOrder(self, root: TreeNode) -> List[List[int]]:
        res = []
        queue =[root]
        if not root:
            return []
        while queue:
            length = len(queue)
            inlist = []
            for i in range(length):

                curnode = queue.pop(0)
                inlist.append(curnode.val)
                if curnode.left : queue.append(curnode.left)
                if curnode.right: queue.append(curnode.right)
            res.append(inlist)
        return res

可以刷的题:
在这里插入图片描述剑指 Offer 32 - I. 从上到下打印二叉树

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值