LeetCode刷题指南
第 0 章 hot100
0.1 哈希
0.2 双指针
0.3 滑动窗口
0.4 子串
0.5 普通数组
0.6 矩阵
0.7 链表
0.8 二叉树
0.9 图论
0.10 回溯
0.11 二分查找
0.12 栈
0.13 堆
0.14 贪心算法
0.15 动态规划
0.16 多维动态规划
0.17 技巧
第0-1章 面试经典150
0.1 数组/字符串
0.2 双指针
0.3 滑动窗口
链表
二叉树
第 1 章 最易懂的贪心算法
1.1 算法解释
1.2 分配问题
1.3 区间问题
1.4 练习
第 2 章 玩转双指针
2.1 算法解释
2.2 Two Sum
2.3 归并两个有序数组
2.4 滑动窗口
2.5 快慢指针
2.6 练习
第 3 章 居合斩!二分查找
3.1 算法解释
3.2 求开方
3.3 查找区间
3.4 查找峰值
3.5 旋转数组查找数字
3.6 练习
第 4 章 千奇百怪的排序算法
4.1 常用排序算法
4.2 快速选择
4.3 桶排序
4.4 练习
第 5 章 一切皆可搜索
5.1 算法解释
5.2 深度优先搜索
5.3 回溯法
5.4 广度优先搜索
5.5 练习
第 6 章 深入浅出动态规划
6.1 算法解释
6.2 基本动态规划:一维
6.3 基本动态规划:二维
6.4 分割类型题
6.5 子序列问题
6.6 背包问题
6.7 字符串编辑
6.8 股票交易
6.9 练习
第 7 章 化繁为简的分治法
7.1 算法解释
7.2 表达式问题
7.3 练习
第 8 章 巧解数学问题
8.1 引言
8.2 公倍数与公因数
8.3 质数
8.4 数字处理
8.5 随机与取样
8.6 练习
第 9 章 神奇的位运算
9.1 常用技巧
9.2 位运算基础问题
9.3 二进制特性
9.4 练习
第 10 章 妙用数据结构
10.1 C++ STL
10.2 Python 常用数据结构
10.3 数组
10.4 栈和队列
10.5 单调栈
10.6 优先队列
10.7 双端队列
10.8 哈希表
10.9 多重集合和映射
10.10 前缀和与积分图
10.11 练习
第 11 章 令人头大的字符串
11.1 引言
11.2 字符串比较
11.3 字符串理解
11.4 字符串匹配
11.5 练习
第 12 章 指针三剑客之一:链表
12.1 数据结构介绍
12.2 链表的基本操作
12.3 其它链表技巧
12.4 练习
第 13 章 指针三剑客之二:树
13.1 数据结构介绍
13.2 树的递归
13.3 层次遍历
13.4 前中后序遍历
13.5 二叉查找树
13.6 字典树
13.7 练习
第 14 章 指针三剑客之三:图
14.1 数据结构介绍
14.2 二分图
14.3 拓扑排序
14.4 练习
第 15 章 更加复杂的数据结构
15.1 引言
15.2 并查集
15.3 复合数据结构
15.4 练习
第16章 面试题
第 17 章 十大经典排序算法
README
本文档使用 MrDoc 发布
-
+
首页
13.7 练习
# 13.7 练习 ## 基础难度 ### [226. Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) 巧用递归,你可以在五行内完成这道题。 --- ### [617. Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) 同样的,利用递归可以轻松搞定。 --- ### [572. Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/) 子树是对称树的姊妹题,写法也十分类似。 --- ### [404. Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) 怎么判断一个节点是不是左节点呢?一种可行的方法是,在辅函数里多传一个参数,表示当前节点是不是父节点的左节点。 --- ### [513. Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/) 最左下角的节点满足什么条件?针对这种条件,我们该如何找到它? BFS 这棵二叉树,先把右儿子入队,再把左儿子入队,这样最后一个出队的节点就是左下角的节点了。 ```py class Solution: def findBottomLeftValue(self, root: Optional[TreeNode]) -> int: q = collections.deque() q.append(root) while q: node = q.popleft() if node.right: q.append(node.right) if node.left: q.append(node.left) return node.val ``` --- ### [538. Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/) 尝试利用某种遍历方式来解决此题,每个节点只需遍历一次。 递归的顺序:右子树-根-左子树。 ```py class Solution: def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: total = 0 def dfs(node): nonlocal total if not node: return dfs(node.right) total += node.val node.val = total dfs(node.left) dfs(root) return root ``` --- ### [235. Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) 利用 BST 的独特性质,这道题可以很轻松完成。 --- ### [530. Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/) 还记得我们所说的,对于 BST 应该利用哪种遍历吗? ```py class Solution: def getMinimumDifference(self, root: Optional[TreeNode]) -> int: minerror = inf pre = -inf def dfs(node): nonlocal minerror,pre if not node: return dfs(node.left) minerror = min(minerror, abs(node.val-pre)) pre = node.val dfs(node.right) dfs(root) return minerror ``` --- ## 进阶难度 ### [1530. Number of Good Leaf Nodes Pairs](https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/) 题目 543 的变种题,注意在辅函数中,每次更新的全局变量是左右两边距离之和满足条件的数量,而返回的是左右两边所有(长度不溢出的)子节点的高度 +1。 --- ### [889. Construct Binary Tree from Preorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/) 给定任意两种遍历结果,我们都可以重建树的结构。 --- ### [106. Construct Binary Tree from Inorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/) 给定任意两种遍历结果,我们都可以重建树的结构。 --- ### [94. Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) 因为前中序后遍历是用递归实现的,而递归的底层实现是栈操作,因此我们总能用栈实现。 --- ### [145. Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/) 因为前中序后遍历是用递归实现的,而递归的底层实现是栈操作,因此我们总能用栈实现。 --- ### [236. Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/) 现在不是 BST,而是普通的二叉树了,该怎么办? --- ### [109. Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) 把排好序的链表变成 BST。为了使得 BST 尽量平衡,我们需要寻找链表的中点。 --- ### [897. Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree/) 把 BST 压成一个链表,务必考虑清楚指针操作的顺序,否则可能会出现环路。 --- ### [653. Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) 啊哈,这道题可能会把你骗到。 --- ### [450. Delete Node in a BST](https://leetcode.com/problems/delete-node-in-a-bst/) 当寻找到待删节点时,你可以分情况考虑——当前节点是叶节点、只有一个子节点和有两个子节点。建议同时回收内存。
嘉心糖糖
2025年3月13日 22:31
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
PDF文档(打印)
分享
链接
类型
密码
更新密码