Skip to content

Commit da5c2f0

Browse files
committed
update content
1 parent a234603 commit da5c2f0

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

数据结构系列/二叉树总结.md

+6
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ void traverse(TreeNode root) {
282282
}
283283
```
284284

285+
<visual slug='mydata-maxdepth1' title='遍历思路的可视化' />
286+
285287
这个解法应该很好理解,但为什么需要在前序位置增加 `depth`,在后序位置减小 `depth`
286288

287289
因为前面说了,前序位置是进入一个节点的时候,后序位置是离开一个节点的时候,`depth` 记录当前递归到的节点深度,你把 `traverse` 理解成在二叉树上游走的一个指针,所以当然要这样维护。
@@ -310,6 +312,8 @@ int maxDepth(TreeNode root) {
310312
}
311313
```
312314

315+
<visual slug='mydata-maxdepth2' title='分解问题思路的可视化' />
316+
313317
只要明确递归函数的定义,这个解法也不难理解,但为什么主要的代码逻辑集中在后序位置?
314318

315319
因为这个思路正确的核心在于,你确实可以通过子树的最大深度推导出原树的深度,所以当然要首先利用递归函数的定义算出左右子树的最大深度,然后推出原树的最大深度,主要逻辑自然放在后序位置。
@@ -548,6 +552,8 @@ class Solution {
548552
}
549553
```
550554

555+
<visual slug='mydata-diameter-of-binary-tree'/>
556+
551557
这下时间复杂度只有 `maxDepth` 函数的 O(N) 了。
552558

553559
讲到这里,照应一下前文:遇到子树问题,首先想到的是给函数设置返回值,然后在后序位置做文章。

数据结构系列/二叉树系列1.md

+2
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ void flatten(TreeNode root) {
341341
}
342342
```
343343

344+
<visual slug='flatten-binary-tree-to-linked-list' />
345+
344346
你看,这就是递归的魅力,你说 `flatten` 函数是怎么把左右子树拉平的?
345347

346348
不容易说清楚,但是只要知道 `flatten` 的定义如此并利用这个定义,让每一个节点做它该做的事情,然后 `flatten` 函数就会按照定义工作。

算法思维系列/双指针技巧.md

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ int removeDuplicates(int[] nums) {
9393
}
9494
```
9595

96+
<visual slug='remove-duplicates-from-sorted-array' />
97+
9698
算法执行的过程如下 GIF 图:
9799

98100
![](https://labuladong.github.io/pictures/数组去重/1.gif)

0 commit comments

Comments
 (0)