-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0145.cpp
More file actions
33 lines (29 loc) · 760 Bytes
/
Copy path0145.cpp
File metadata and controls
33 lines (29 loc) · 760 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
if (!root) return {};
vector<int> res;
unordered_set<TreeNode *> s;
stack<TreeNode *> st;
while (root) {
st.push(root);
root = root->left;
}
while (!st.empty()) {
TreeNode *root = st.top();
st.pop();
if (!s.count(root)) {
s.insert(root);
st.push(root);
root = root->right;
while (root) {
st.push(root);
root = root->left;
}
} else {
res.push_back(root->val);
}
}
return res;
}
};