Skip to content

Commit 34bc76c

Browse files
authored
Merge pull request #222 from LetMeFly666/145
update: 0145's 题解&代码
2 parents 53e87de + ea34e17 commit 34bc76c

5 files changed

+241
-2
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-02-12 09:53:55
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-02-12 09:54:57
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
/**
12+
* Definition for a binary tree node.
13+
* struct TreeNode {
14+
* int val;
15+
* TreeNode *left;
16+
* TreeNode *right;
17+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
18+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
19+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
20+
* };
21+
*/
22+
class Solution {
23+
private:
24+
vector<int> ans;
25+
26+
void dfs(TreeNode* root) {
27+
if (!root) {
28+
return;
29+
}
30+
dfs(root->left);
31+
dfs(root->right);
32+
ans.push_back(root->val);
33+
}
34+
35+
public:
36+
vector<int> postorderTraversal(TreeNode* root) {
37+
dfs(root);
38+
return ans;
39+
}
40+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-02-12 09:59:42
4+
LastEditors: LetMeFly
5+
LastEditTime: 2024-02-12 10:00:54
6+
'''
7+
from typing import List, Optional
8+
9+
# Definition for a binary tree node.
10+
class TreeNode:
11+
def __init__(self, val=0, left=None, right=None):
12+
self.val = val
13+
self.left = left
14+
self.right = right
15+
16+
class Solution:
17+
def dfs(self, root: Optional[TreeNode]) -> None:
18+
if not root:
19+
return
20+
self.dfs(root.left)
21+
self.dfs(root.right)
22+
self.ans.append(root.val)
23+
24+
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
25+
self.ans = []
26+
self.dfs(root)
27+
return self.ans
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-02-12 09:56:30
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-02-12 09:58:46
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
/**
12+
* Definition for a binary tree node.
13+
* struct TreeNode {
14+
* int val;
15+
* TreeNode *left;
16+
* TreeNode *right;
17+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
18+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
19+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
20+
* };
21+
*/
22+
class Solution {
23+
public:
24+
vector<int> postorderTraversal(TreeNode* root) {
25+
vector<int> ans;
26+
stack<pair<TreeNode*, bool>> st;
27+
st.push({root, false});
28+
while (st.size()) {
29+
auto [thisNode, ifPushed] = st.top();
30+
st.pop();
31+
if (!thisNode) {
32+
continue;
33+
}
34+
if (ifPushed) {
35+
ans.push_back(thisNode->val);
36+
}
37+
else {
38+
st.push({thisNode, true});
39+
st.push({thisNode->right, false});
40+
st.push({thisNode->left, false});
41+
}
42+
}
43+
return ans;
44+
}
45+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-02-12 10:01:34
4+
LastEditors: LetMeFly
5+
LastEditTime: 2024-02-12 10:03:03
6+
'''
7+
from typing import List, Optional
8+
9+
# Definition for a binary tree node.
10+
class TreeNode:
11+
def __init__(self, val=0, left=None, right=None):
12+
self.val = val
13+
self.left = left
14+
self.right = right
15+
16+
class Solution:
17+
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
18+
ans = []
19+
st = [(root, False)]
20+
while st:
21+
thisNode, ifPushed = st.pop()
22+
if not thisNode:
23+
continue
24+
if ifPushed:
25+
ans.append(thisNode.val)
26+
else:
27+
st.append((thisNode, True))
28+
st.append((thisNode.right, False))
29+
st.append((thisNode.left, False))
30+
return ans

Solutions/LeetCode 0145.二叉树的后序遍历.md

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ date: 2022-07-29 15:50:29
44
tags: [题解, LeetCode, 简单, 栈, 树, 深度优先搜索, 二叉树, DFS, 后序遍历]
55
---
66

7-
# 【LetMeFly】145.二叉树的后序遍历:二叉树必会算法
7+
# 【LetMeFly】145.二叉树的后序遍历:二叉树必会算法-递归/迭代(栈模拟递归)
88

99
力扣题目链接:[https://leetcode.cn/problems/binary-tree-postorder-traversal/](https://leetcode.cn/problems/binary-tree-postorder-traversal/)
1010

@@ -48,7 +48,7 @@ tags: [题解, LeetCode, 简单, 栈, 树, 深度优先搜索, 二叉树, DFS,
4848

4949

5050

51-
## 方法一:DFS
51+
## 方法一:深度优先搜索DFS(递归)
5252

5353
在学习后序遍历之前,有必要先了解以下[前序遍历](https://blog.tisfy.eu.org/2022/07/29/LeetCode%200144.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%89%8D%E5%BA%8F%E9%81%8D%E5%8E%86/)
5454

@@ -112,5 +112,102 @@ public:
112112
};
113113
```
114114

115+
#### Python
116+
117+
```python
118+
# from typing import List, Optional
119+
120+
# # Definition for a binary tree node.
121+
# class TreeNode:
122+
# def __init__(self, val=0, left=None, right=None):
123+
# self.val = val
124+
# self.left = left
125+
# self.right = right
126+
127+
class Solution:
128+
def dfs(self, root: Optional[TreeNode]) -> None:
129+
if not root:
130+
return
131+
self.dfs(root.left)
132+
self.dfs(root.right)
133+
self.ans.append(root.val)
134+
135+
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
136+
self.ans = []
137+
self.dfs(root)
138+
return self.ans
139+
```
140+
141+
## 方法二:使用栈模拟递归(栈模拟递归)
142+
143+
使用栈模拟递归,具体做法可参考[94. 中序遍历](https://leetcode.letmefly.xyz/2024/02/10/LeetCode%200094.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E4%B8%AD%E5%BA%8F%E9%81%8D%E5%8E%86/#方法二:使用栈模拟递归(栈模拟递归))
144+
145+
与之不同的是,出栈顺序应该是左子右子根,因此入栈顺序为根右子左子。
146+
147+
+ 时间复杂度$O(N)$,其中$N$是二叉树节点的个数
148+
+ 空间复杂度$O(N)$
149+
150+
### AC代码
151+
152+
#### C++
153+
154+
```cpp
155+
class Solution {
156+
public:
157+
vector<int> postorderTraversal(TreeNode* root) {
158+
vector<int> ans;
159+
stack<pair<TreeNode*, bool>> st;
160+
st.push({root, false});
161+
while (st.size()) {
162+
auto [thisNode, ifPushed] = st.top();
163+
st.pop();
164+
if (!thisNode) {
165+
continue;
166+
}
167+
if (ifPushed) {
168+
ans.push_back(thisNode->val);
169+
}
170+
else {
171+
st.push({thisNode, true});
172+
st.push({thisNode->right, false});
173+
st.push({thisNode->left, false});
174+
}
175+
}
176+
return ans;
177+
}
178+
};
179+
```
180+
181+
#### Python
182+
183+
```python
184+
# from typing import List, Optional
185+
186+
# # Definition for a binary tree node.
187+
# class TreeNode:
188+
# def __init__(self, val=0, left=None, right=None):
189+
# self.val = val
190+
# self.left = left
191+
# self.right = right
192+
193+
class Solution:
194+
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
195+
ans = []
196+
st = [(root, False)]
197+
while st:
198+
thisNode, ifPushed = st.pop()
199+
if not thisNode:
200+
continue
201+
if ifPushed:
202+
ans.append(thisNode.val)
203+
else:
204+
st.append((thisNode, True))
205+
st.append((thisNode.right, False))
206+
st.append((thisNode.left, False))
207+
return ans
208+
209+
```
210+
211+
115212
> 同步发文于CSDN,原创不易,转载请附上[原文链接](https://blog.tisfy.eu.org/2022/07/29/LeetCode%200145.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%90%8E%E5%BA%8F%E9%81%8D%E5%8E%86/)哦~
116213
> Tisfy:[https://letmefly.blog.csdn.net/article/details/126057794](https://letmefly.blog.csdn.net/article/details/126057794)

0 commit comments

Comments
 (0)