forked from fanfank/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-tree-postorder-traversal.cpp
43 lines (43 loc) · 1.22 KB
/
binary-tree-postorder-traversal.cpp
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
34
35
36
37
38
39
40
41
42
43
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {//Trick is that root=>s2, root->right=>s2, root->left=>s2
public:
vector<int> v;
vector<int> postorderTraversal(TreeNode *root) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
v.clear();
queue<TreeNode*> q;
stack<TreeNode*> s1, s2;
if(root)
q.push(root);
while(!q.empty() || !s1.empty()) {
while(!q.empty()) {
TreeNode *tmp = q.front();
q.pop();
if(tmp->right)
q.push(tmp->right);
s1.push(tmp);
s2.push(tmp);
}
if(!s1.empty()) {
TreeNode *tmp = s1.top();
s1.pop();
if(tmp->left)
q.push(tmp->left);
}
}
while(!s2.empty()) {
v.push_back(s2.top()->val);
s2.pop();
}
return v;
}
};