当前位置: 首页 > news >正文

LeetCode Hot100 刷题笔记(4)—— 二叉树、图论

目录

一、二叉树

1. 二叉树的深度遍历(DFS:前序、中序、后序遍历)

2. 二叉树的最大深度

3. 翻转二叉树

4. 对称二叉树

5. 二叉树的直径

6. 二叉树的层序遍历

7. 将有序数组转换为二叉搜索树

8. 验证二叉搜索树

9. 二叉搜索树中第 K 小的元素

10. 二叉树的右视图

(待更...)

二、图论(待更...)


前言

一、二叉树:二叉树的中序遍历,二叉树的最大深度,翻转二叉树,对称二叉树,二叉树的直径,二叉树的层序遍历,将有序数组转换为二叉搜索树,验证二叉搜索树,二叉搜索树中第 K 小的元素,二叉树的右视图...... (日更ing)

二、图论:...... (日更ing)


一、二叉树

1. 二叉树的深度遍历(DFS:前序、中序、后序遍历)

 原题链接:94. 二叉树的中序遍历 - 力扣(LeetCode)

# (1)前序遍历:根-左-右
class Solution(object):
    def preorderTraversal(self, root):
        res = []
        def preorder(root):
            if not root:
                return 
            res.append(root.val)
            preorder(root.left)
            preorder(root.right)
        preorder(root)
        return res
# (2)中序遍历:左-根-右
class Solution(object):
    def inorderTraversal(self, root):
        res = []
        def inorder(root):
            if not root:
                return 
            inorder(root.left)
            res.append(root.val)
            inorder(root.right)
        inorder(root)
        return res

# (3)后序遍历:左-右-根
class Solution(object):
    def postorderTraversal(self, root):
        res = []
        def inorder(root):
            if not root:
                return 
            postorder(root.left)
            postorder(root.right)
            res.append(root.val)
        postorder(root)
        return res

2. 二叉树的最大深度

原题链接:104. 二叉树的最大深度 - 力扣(LeetCode)

class Solution(object):
    def maxDepth(self, root):
        if not root:
            return 0
        left_height = self.maxDepth(root.left)
        right_height = self.maxDepth(root.right)
        return max(left_height, right_height) + 1

3. 翻转二叉树

原题链接:226. 翻转二叉树 - 力扣(LeetCode)

class Solution(object):
    def invertTree(self, root):
        if not root:
            return 
        root.left, root.right = root.right, root.left
        self.invertTree(root.left)
        self.invertTree(root.right)
        return root

4. 对称二叉树

原题链接:101. 对称二叉树 - 力扣(LeetCode)

class Solution(object):
    def isSymmetric(self, root):
        def check(left, right):
            if not left and not right:
                return True
            if not left or not right:
                return False
            if left.val != right.val:
                return False
            return check(left.left, right.right) and check(left.right, right.left)
        return check(root.left, root.right)

5. 二叉树的直径

原题链接:543. 二叉树的直径 - 力扣(LeetCode)

class Solution(object):
    def diameterOfBinaryTree(self, root):
        def dfs(root):
            if not root:
                return 0, 0
            ld, ldepth = dfs(root.left)
            rd, rdepth = dfs(root.right)
            return max(ld, rd, ldepth+rdepth), max(ldepth, rdepth) + 1
        return dfs(root)[0]

6. 二叉树的层序遍历

原题链接:102. 二叉树的层序遍历 - 力扣(LeetCode)

class Solution(object):
    def levelOrder(self, root):
        if not root:
            return []
        node = [root]
        res = []
        while len(node) > 0:
            res.append([i.val for i in node])
            node2 = []
            for i in node:
                if i.left:
                    node2.append(i.left)
                if i.right:
                    node2.append(i.right)
            node = node2
        return res

7. 将有序数组转换为二叉搜索树

原题链接:108. 将有序数组转换为二叉搜索树 - 力扣(LeetCode)

class Solution(object):
    def sortedArrayToBST(self, nums):
        def dfs(left, right):
            if left > right:
                return
            mid = (left + right) // 2
            root = TreeNode(nums[mid])
            root.left = dfs(left, mid-1)
            root.right = dfs(mid+1, right)
            return root
        return dfs(0, len(nums)-1)

8. 验证二叉搜索树

原题链接:98. 验证二叉搜索树 - 力扣(LeetCode)

class Solution(object):
    def isValidBST(self, root, left=float('-inf'), right=float('inf')):
        if not root:
            return True
        x = root.val
        return left < x < right and self.isValidBST(root.left, left, x) and self.isValidBST(root.right, x, right)
# self.isValidBST(root.left, left, x):遍历左子树,右边界更新
# self.isValidBST(root.right, x, right):遍历右子树,左边界更新

9. 二叉搜索树中第 K 小的元素

原题链接:230. 二叉搜索树中第 K 小的元素 - 力扣(LeetCode)

# 先通过前序/中序/后序遍历转为list,而后利用list属性找第k个小的元素。
# 此代码使用前序遍历(根-左-右)
class Solution(object):
    def kthSmallest(self, root, k):
        res = [] 
        def preorder(root):
            if not root:
                return
            res.append(root.val)
            preorder(root.left)
            preorder(root.right)
            return res
        res = preorder(root)
        res.sort()
        return res[k-1]

10. 二叉树的右视图

原题链接:199. 二叉树的右视图 - 力扣(LeetCode)

class Solution(object):
    def rightSideView(self, root):
        # if len(res) == depth: res.append(root.val)
        # 先遍历右子树,再遍历左子树
        res = []
        def dfs(root, depth):
            if not root:
                return []
            if len(res) == depth:
                res.append(root.val)
            dfs(root.right, depth+1)
            dfs(root.left, depth+1)
            return res
        return dfs(root, 0)


二、图论

相关文章:

  • PyTorch实现Transformer模型
  • 输出输入练习
  • 《数字图像处理》第四章 频率域滤波简要学习笔记以及频率域滤波与空间域滤波的区别
  • 构建稳健的机器学习系统:应对数据偏移挑战
  • Leetcode 交错字符串
  • [FPGA基础学习]加法器、三八译码器及DE2-115基本使用方法和数码管显示
  • (C语言)动态分配的动态通讯录(静态通讯录Plus)(C语言小项目)
  • 关于跨域问题(本地前端访问服务器端接口跨域出错)
  • Notepad++ 替换 换行符 为 逗号
  • 关于服务器只能访问localhost:8111地址,局域网不能访问的问题
  • AWE直击:萤石RX30 Max的吸泡面战争,一场清洁技术的范式革命
  • 分布式服务的熔断和降级
  • Unity Shader 学习17:合批渲染
  • Spring Boot 连接 MySQL 配置参数详解
  • 维创智脑(WIC)项目观察:技术集成的理想模型与现实难题
  • Containerd+Kubernetes搭建k8s集群
  • Mysql表中的字符编码到底怎么设置
  • ref和reactive区别
  • JS输出九九乘法表
  • Flink watermark的时间字段有空值,建表的时候,如何处理
  • v9双语版网站怎么做/百度seo优化排名
  • 内衣批发网站建设/百度注册公司地址
  • 六灶网站建设/百度关键词在线优化
  • 有哪些做的很漂亮的网站/百度游戏风云榜
  • 管理咨询公司经营范围包括哪些/优化是什么意思?
  • 巴楚网站建设/店铺运营方案策划