35
35
36
36
先序遍历的顺序是:头、左、右,如果我们改变左右孩子的顺序,就能将顺序变成:头、右、左。
37
37
38
- 我们先不打印头节点,而是存放到另一个收集栈 res 中,最后遍历结束,输出收集栈元素,即是后序遍历:左、右、头。收集栈是为了实现结果列表的逆序。我们也可以直接使用链表,每次插入元素时,放在头部,最后直接返回链表即可,无需进行逆序。
38
+ 我们先不打印头节点,而是存放到另一个收集栈 ans 中,最后遍历结束,输出收集栈元素,即是后序遍历:左、右、头。收集栈是为了实现结果列表的逆序。我们也可以直接使用链表,每次插入元素时,放在头部,最后直接返回链表即可,无需进行逆序。
39
39
40
40
** 3. Morris 实现后序遍历**
41
41
42
42
Morris 遍历无需使用栈,空间复杂度为 O(1)。核心思想是:
43
43
44
44
遍历二叉树节点,
45
45
46
- 1 . 若当前节点 root 的右子树为空,** 将当前节点值添加至结果列表 res ** 中,并将当前节点更新为 ` root.left `
46
+ 1 . 若当前节点 root 的右子树为空,** 将当前节点值添加至结果列表 ans ** 中,并将当前节点更新为 ` root.left `
47
47
2 . 若当前节点 root 的右子树不为空,找到右子树的最左节点 next(也即是 root 节点在中序遍历下的后继节点):
48
- - 若后继节点 next 的左子树为空,** 将当前节点值添加至结果列表 res ** 中,然后将后继节点的左子树指向当前节点 root,并将当前节点更新为 ` root.right ` 。
48
+ - 若后继节点 next 的左子树为空,** 将当前节点值添加至结果列表 ans ** 中,然后将后继节点的左子树指向当前节点 root,并将当前节点更新为 ` root.right ` 。
49
49
- 若后继节点 next 的左子树不为空,将后继节点左子树指向空(即解除 next 与 root 的指向关系),并将当前节点更新为 ` root.left ` 。
50
50
3 . 循环以上步骤,直至二叉树节点为空,遍历结束。
51
51
4 . 最后返回结果列表的逆序即可。
@@ -68,17 +68,18 @@ Morris 遍历无需使用栈,空间复杂度为 O(1)。核心思想是:
68
68
# self.left = left
69
69
# self.right = right
70
70
class Solution :
71
- def postorderTraversal (self , root : TreeNode) -> List[int ]:
72
- res = []
73
-
74
- def postorder (root ):
75
- if root:
76
- postorder(root.left)
77
- postorder(root.right)
78
- res.append(root.val)
79
-
80
- postorder(root)
81
- return res
71
+ def postorderTraversal (self , root : Optional[TreeNode]) -> List[int ]:
72
+ def dfs (root ):
73
+ if root is None :
74
+ return
75
+ dfs(root.left)
76
+ dfs(root.right)
77
+ nonlocal ans
78
+ ans.append(root.val)
79
+
80
+ ans = []
81
+ dfs(root)
82
+ return ans
82
83
```
83
84
84
85
栈实现非递归:
@@ -91,18 +92,19 @@ class Solution:
91
92
# self.left = left
92
93
# self.right = right
93
94
class Solution :
94
- def postorderTraversal (self , root : TreeNode) -> List[int ]:
95
- res = []
96
- if root:
97
- s = [root]
98
- while s:
99
- node = s.pop()
100
- res.append(node.val)
101
- if node.left:
102
- s.append(node.left)
103
- if node.right:
104
- s.append(node.right)
105
- return res[::- 1 ]
95
+ def postorderTraversal (self , root : Optional[TreeNode]) -> List[int ]:
96
+ ans = []
97
+ if root is None :
98
+ return ans
99
+ stk = [root]
100
+ while stk:
101
+ node = stk.pop()
102
+ ans.append(node.val)
103
+ if node.left:
104
+ stk.append(node.left)
105
+ if node.right:
106
+ stk.append(node.right)
107
+ return ans[::- 1 ]
106
108
```
107
109
108
110
Morris 遍历:
@@ -115,24 +117,24 @@ Morris 遍历:
115
117
# self.left = left
116
118
# self.right = right
117
119
class Solution :
118
- def postorderTraversal (self , root : TreeNode) -> List[int ]:
119
- res = []
120
+ def postorderTraversal (self , root : Optional[ TreeNode] ) -> List[int ]:
121
+ ans = []
120
122
while root:
121
123
if root.right is None :
122
- res .append(root.val)
124
+ ans .append(root.val)
123
125
root = root.left
124
126
else :
125
127
next = root.right
126
128
while next .left and next .left != root:
127
129
next = next .left
128
- if next .left is None :
129
- res .append(root.val)
130
+ if next .left != root :
131
+ ans .append(root.val)
130
132
next .left = root
131
133
root = root.right
132
134
else :
133
135
next .left = None
134
136
root = root.left
135
- return res [::- 1 ]
137
+ return ans [::- 1 ]
136
138
```
137
139
138
140
### ** Java**
@@ -158,18 +160,21 @@ class Solution:
158
160
* }
159
161
*/
160
162
class Solution {
163
+ private List<Integer > ans;
164
+
161
165
public List<Integer > postorderTraversal (TreeNode root ) {
162
- List< Integer > res = new ArrayList<> ();
163
- postorder (root, res );
164
- return res ;
166
+ ans = new ArrayList<> ();
167
+ dfs (root);
168
+ return ans ;
165
169
}
166
170
167
- private void postorder (TreeNode root , List<Integer > res ) {
168
- if (root != null ) {
169
- postorder(root. left, res);
170
- postorder(root. right, res);
171
- res. add(root. val);
171
+ private void dfs (TreeNode root ) {
172
+ if (root == null ) {
173
+ return ;
172
174
}
175
+ dfs(root. left);
176
+ dfs(root. right);
177
+ ans. add(root. val);
173
178
}
174
179
}
175
180
```
@@ -194,22 +199,23 @@ class Solution {
194
199
*/
195
200
class Solution {
196
201
public List<Integer > postorderTraversal (TreeNode root ) {
197
- LinkedList<Integer > res = new LinkedList<> ();
198
- if (root != null ) {
199
- Deque<TreeNode > s = new LinkedList<> ();
200
- s. offerLast(root);
201
- while (! s. isEmpty()) {
202
- TreeNode node = s. pollLast();
203
- res. addFirst(node. val);
204
- if (node. left != null ) {
205
- s. offerLast(node. left);
206
- }
207
- if (node. right != null ) {
208
- s. offerLast(node. right);
209
- }
202
+ LinkedList<Integer > ans = new LinkedList<> ();
203
+ if (root == null ) {
204
+ return ans;
205
+ }
206
+ Deque<TreeNode > stk = new ArrayDeque<> ();
207
+ stk. push(root);
208
+ while (! stk. isEmpty()) {
209
+ TreeNode node = stk. pop();
210
+ ans. addFirst(node. val);
211
+ if (node. left != null ) {
212
+ stk. push(node. left);
213
+ }
214
+ if (node. right != null ) {
215
+ stk. push(node. right);
210
216
}
211
217
}
212
- return res ;
218
+ return ans ;
213
219
}
214
220
}
215
221
```
@@ -234,18 +240,18 @@ Morris 遍历:
234
240
*/
235
241
class Solution {
236
242
public List<Integer > postorderTraversal (TreeNode root ) {
237
- LinkedList<Integer > res = new LinkedList<> ();
243
+ LinkedList<Integer > ans = new LinkedList<> ();
238
244
while (root != null ) {
239
245
if (root. right == null ) {
240
- res . addFirst(root. val);
246
+ ans . addFirst(root. val);
241
247
root = root. left;
242
248
} else {
243
249
TreeNode next = root. right;
244
250
while (next. left != null && next. left != root) {
245
251
next = next. left;
246
252
}
247
253
if (next. left == null ) {
248
- res . addFirst(root. val);
254
+ ans . addFirst(root. val);
249
255
next. left = root;
250
256
root = root. right;
251
257
} else {
@@ -254,7 +260,7 @@ class Solution {
254
260
}
255
261
}
256
262
}
257
- return res ;
263
+ return ans ;
258
264
}
259
265
}
260
266
```
@@ -316,25 +322,25 @@ function postorderTraversal(root: TreeNode | null): number[] {
316
322
*/
317
323
class Solution {
318
324
public:
319
- vector<int > postorderTraversal(TreeNode * root) {
320
- vector<int > res ;
325
+ vector<int > postorderTraversal(TreeNode* root) {
326
+ vector<int > ans ;
321
327
while (root)
322
328
{
323
- if (root->right == nullptr )
329
+ if (! root->right)
324
330
{
325
- res .push_back(root->val);
331
+ ans .push_back(root->val);
326
332
root = root->left;
327
333
}
328
334
else
329
335
{
330
- TreeNode * next = root->right;
336
+ TreeNode* next = root->right;
331
337
while (next->left && next->left != root)
332
338
{
333
339
next = next->left;
334
340
}
335
- if (next->left == nullptr )
341
+ if (! next->left)
336
342
{
337
- res .push_back(root->val);
343
+ ans .push_back(root->val);
338
344
next->left = root;
339
345
root = root->right;
340
346
}
@@ -345,8 +351,8 @@ public:
345
351
}
346
352
}
347
353
}
348
- reverse(res .begin(), res .end());
349
- return res ;
354
+ reverse(ans .begin(), ans .end());
355
+ return ans ;
350
356
}
351
357
};
352
358
```
@@ -363,18 +369,18 @@ public:
363
369
* }
364
370
*/
365
371
func postorderTraversal(root *TreeNode) []int {
366
- var res []int
372
+ var ans []int
367
373
for root != nil {
368
374
if root.Right == nil {
369
- res = append([]int{root.Val}, res ...)
375
+ ans = append([]int{root.Val}, ans ...)
370
376
root = root.Left
371
377
} else {
372
378
next := root.Right
373
379
for next.Left != nil && next.Left != root {
374
380
next = next.Left
375
381
}
376
382
if next.Left == nil {
377
- res = append([]int{root.Val}, res ...)
383
+ ans = append([]int{root.Val}, ans ...)
378
384
next.Left = root
379
385
root = root.Right
380
386
} else {
@@ -383,7 +389,7 @@ func postorderTraversal(root *TreeNode) []int {
383
389
}
384
390
}
385
391
}
386
- return res
392
+ return ans
387
393
}
388
394
```
389
395
0 commit comments