diff --git "a/346円225円260円346円215円256円347円273円223円346円236円204円347円263円273円345円210円227円/344円272円214円345円217円211円346円240円221円346円200円273円347円273円223円.md" "b/346円225円260円346円215円256円347円273円223円346円236円204円347円263円273円345円210円227円/344円272円214円345円217円211円346円240円221円346円200円273円347円273円223円.md"
index 8157c801aa..6cfb9f0146 100644
--- "a/346円225円260円346円215円256円347円273円223円346円236円204円347円263円273円345円210円227円/344円272円214円345円217円211円346円240円221円346円200円273円347円273円223円.md"
+++ "b/346円225円260円346円215円256円347円273円223円346円236円204円347円263円273円345円210円227円/344円272円214円345円217円211円346円240円221円346円200円273円347円273円223円.md"
@@ -282,6 +282,8 @@ void traverse(TreeNode root) {
}
```
+
+
这个解法应该很好理解,但为什么需要在前序位置增加 `depth`,在后序位置减小 `depth`?
因为前面说了,前序位置是进入一个节点的时候,后序位置是离开一个节点的时候,`depth` 记录当前递归到的节点深度,你把 `traverse` 理解成在二叉树上游走的一个指针,所以当然要这样维护。
@@ -310,6 +312,8 @@ int maxDepth(TreeNode root) {
}
```
+
+
只要明确递归函数的定义,这个解法也不难理解,但为什么主要的代码逻辑集中在后序位置?
因为这个思路正确的核心在于,你确实可以通过子树的最大深度推导出原树的深度,所以当然要首先利用递归函数的定义算出左右子树的最大深度,然后推出原树的最大深度,主要逻辑自然放在后序位置。
@@ -548,6 +552,8 @@ class Solution {
}
```
+
+
这下时间复杂度只有 `maxDepth` 函数的 O(N) 了。
讲到这里,照应一下前文:遇到子树问题,首先想到的是给函数设置返回值,然后在后序位置做文章。
diff --git "a/346円225円260円346円215円256円347円273円223円346円236円204円347円263円273円345円210円227円/344円272円214円345円217円211円346円240円221円347円263円273円345円210円2271円.md" "b/346円225円260円346円215円256円347円273円223円346円236円204円347円263円273円345円210円227円/344円272円214円345円217円211円346円240円221円347円263円273円345円210円2271円.md"
index de387eb29c..00366cec23 100644
--- "a/346円225円260円346円215円256円347円273円223円346円236円204円347円263円273円345円210円227円/344円272円214円345円217円211円346円240円221円347円263円273円345円210円2271円.md"
+++ "b/346円225円260円346円215円256円347円273円223円346円236円204円347円263円273円345円210円227円/344円272円214円345円217円211円346円240円221円347円263円273円345円210円2271円.md"
@@ -341,6 +341,8 @@ void flatten(TreeNode root) {
}
```
+
+
你看,这就是递归的魅力,你说 `flatten` 函数是怎么把左右子树拉平的?
不容易说清楚,但是只要知道 `flatten` 的定义如此并利用这个定义,让每一个节点做它该做的事情,然后 `flatten` 函数就会按照定义工作。
diff --git "a/347円256円227円346円263円225円346円200円235円347円273円264円347円263円273円345円210円227円/345円217円214円346円214円207円351円222円210円346円212円200円345円267円247円.md" "b/347円256円227円346円263円225円346円200円235円347円273円264円347円263円273円345円210円227円/345円217円214円346円214円207円351円222円210円346円212円200円345円267円247円.md"
index 7c35790e67..4e9ea6cb4e 100644
--- "a/347円256円227円346円263円225円346円200円235円347円273円264円347円263円273円345円210円227円/345円217円214円346円214円207円351円222円210円346円212円200円345円267円247円.md"
+++ "b/347円256円227円346円263円225円346円200円235円347円273円264円347円263円273円345円210円227円/345円217円214円346円214円207円351円222円210円346円212円200円345円267円247円.md"
@@ -93,6 +93,8 @@ int removeDuplicates(int[] nums) {
}
```
+
+
算法执行的过程如下 GIF 图:
