Skip to content

Commit de76b3f

Browse files
committed
feat: update solutions to lc problem: No.0145
No.0145.Binary Tree Postorder Traversal
1 parent 369fdb1 commit de76b3f

File tree

6 files changed

+174
-162
lines changed

6 files changed

+174
-162
lines changed

solution/0100-0199/0145.Binary Tree Postorder Traversal/README.md

+77-71
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@
3535

3636
先序遍历的顺序是:头、左、右,如果我们改变左右孩子的顺序,就能将顺序变成:头、右、左。
3737

38-
我们先不打印头节点,而是存放到另一个收集栈 res 中,最后遍历结束,输出收集栈元素,即是后序遍历:左、右、头。收集栈是为了实现结果列表的逆序。我们也可以直接使用链表,每次插入元素时,放在头部,最后直接返回链表即可,无需进行逆序。
38+
我们先不打印头节点,而是存放到另一个收集栈 ans 中,最后遍历结束,输出收集栈元素,即是后序遍历:左、右、头。收集栈是为了实现结果列表的逆序。我们也可以直接使用链表,每次插入元素时,放在头部,最后直接返回链表即可,无需进行逆序。
3939

4040
**3. Morris 实现后序遍历**
4141

4242
Morris 遍历无需使用栈,空间复杂度为 O(1)。核心思想是:
4343

4444
遍历二叉树节点,
4545

46-
1. 若当前节点 root 的右子树为空,**将当前节点值添加至结果列表 res** 中,并将当前节点更新为 `root.left`
46+
1. 若当前节点 root 的右子树为空,**将当前节点值添加至结果列表 ans** 中,并将当前节点更新为 `root.left`
4747
2. 若当前节点 root 的右子树不为空,找到右子树的最左节点 next(也即是 root 节点在中序遍历下的后继节点):
48-
- 若后继节点 next 的左子树为空,**将当前节点值添加至结果列表 res** 中,然后将后继节点的左子树指向当前节点 root,并将当前节点更新为 `root.right`
48+
- 若后继节点 next 的左子树为空,**将当前节点值添加至结果列表 ans** 中,然后将后继节点的左子树指向当前节点 root,并将当前节点更新为 `root.right`
4949
- 若后继节点 next 的左子树不为空,将后继节点左子树指向空(即解除 next 与 root 的指向关系),并将当前节点更新为 `root.left`
5050
3. 循环以上步骤,直至二叉树节点为空,遍历结束。
5151
4. 最后返回结果列表的逆序即可。
@@ -68,17 +68,18 @@ Morris 遍历无需使用栈,空间复杂度为 O(1)。核心思想是:
6868
# self.left = left
6969
# self.right = right
7070
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
8283
```
8384

8485
栈实现非递归:
@@ -91,18 +92,19 @@ class Solution:
9192
# self.left = left
9293
# self.right = right
9394
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]
106108
```
107109

108110
Morris 遍历:
@@ -115,24 +117,24 @@ Morris 遍历:
115117
# self.left = left
116118
# self.right = right
117119
class Solution:
118-
def postorderTraversal(self, root: TreeNode) -> List[int]:
119-
res = []
120+
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
121+
ans = []
120122
while root:
121123
if root.right is None:
122-
res.append(root.val)
124+
ans.append(root.val)
123125
root = root.left
124126
else:
125127
next = root.right
126128
while next.left and next.left != root:
127129
next = next.left
128-
if next.left is None:
129-
res.append(root.val)
130+
if next.left != root:
131+
ans.append(root.val)
130132
next.left = root
131133
root = root.right
132134
else:
133135
next.left = None
134136
root = root.left
135-
return res[::-1]
137+
return ans[::-1]
136138
```
137139

138140
### **Java**
@@ -158,18 +160,21 @@ class Solution:
158160
* }
159161
*/
160162
class Solution {
163+
private List<Integer> ans;
164+
161165
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;
165169
}
166170

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;
172174
}
175+
dfs(root.left);
176+
dfs(root.right);
177+
ans.add(root.val);
173178
}
174179
}
175180
```
@@ -194,22 +199,23 @@ class Solution {
194199
*/
195200
class Solution {
196201
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);
210216
}
211217
}
212-
return res;
218+
return ans;
213219
}
214220
}
215221
```
@@ -234,18 +240,18 @@ Morris 遍历:
234240
*/
235241
class Solution {
236242
public List<Integer> postorderTraversal(TreeNode root) {
237-
LinkedList<Integer> res = new LinkedList<>();
243+
LinkedList<Integer> ans = new LinkedList<>();
238244
while (root != null) {
239245
if (root.right == null) {
240-
res.addFirst(root.val);
246+
ans.addFirst(root.val);
241247
root = root.left;
242248
} else {
243249
TreeNode next = root.right;
244250
while (next.left != null && next.left != root) {
245251
next = next.left;
246252
}
247253
if (next.left == null) {
248-
res.addFirst(root.val);
254+
ans.addFirst(root.val);
249255
next.left = root;
250256
root = root.right;
251257
} else {
@@ -254,7 +260,7 @@ class Solution {
254260
}
255261
}
256262
}
257-
return res;
263+
return ans;
258264
}
259265
}
260266
```
@@ -316,25 +322,25 @@ function postorderTraversal(root: TreeNode | null): number[] {
316322
*/
317323
class Solution {
318324
public:
319-
vector<int> postorderTraversal(TreeNode *root) {
320-
vector<int> res;
325+
vector<int> postorderTraversal(TreeNode* root) {
326+
vector<int> ans;
321327
while (root)
322328
{
323-
if (root->right == nullptr)
329+
if (!root->right)
324330
{
325-
res.push_back(root->val);
331+
ans.push_back(root->val);
326332
root = root->left;
327333
}
328334
else
329335
{
330-
TreeNode *next = root->right;
336+
TreeNode* next = root->right;
331337
while (next->left && next->left != root)
332338
{
333339
next = next->left;
334340
}
335-
if (next->left == nullptr)
341+
if (!next->left)
336342
{
337-
res.push_back(root->val);
343+
ans.push_back(root->val);
338344
next->left = root;
339345
root = root->right;
340346
}
@@ -345,8 +351,8 @@ public:
345351
}
346352
}
347353
}
348-
reverse(res.begin(), res.end());
349-
return res;
354+
reverse(ans.begin(), ans.end());
355+
return ans;
350356
}
351357
};
352358
```
@@ -363,18 +369,18 @@ public:
363369
* }
364370
*/
365371
func postorderTraversal(root *TreeNode) []int {
366-
var res []int
372+
var ans []int
367373
for root != nil {
368374
if root.Right == nil {
369-
res = append([]int{root.Val}, res...)
375+
ans = append([]int{root.Val}, ans...)
370376
root = root.Left
371377
} else {
372378
next := root.Right
373379
for next.Left != nil && next.Left != root {
374380
next = next.Left
375381
}
376382
if next.Left == nil {
377-
res = append([]int{root.Val}, res...)
383+
ans = append([]int{root.Val}, ans...)
378384
next.Left = root
379385
root = root.Right
380386
} else {
@@ -383,7 +389,7 @@ func postorderTraversal(root *TreeNode) []int {
383389
}
384390
}
385391
}
386-
return res
392+
return ans
387393
}
388394
```
389395

0 commit comments

Comments
 (0)