-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path145.cpp
More file actions
44 lines (39 loc) · 968 Bytes
/
Copy path145.cpp
File metadata and controls
44 lines (39 loc) · 968 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
34
35
36
37
38
39
40
41
42
43
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> path;
if(root==NULL)return path;
stack<TreeNode*> stk;
stk.push(root);
TreeNode* cur = NULL;
while(!stk.empty()) {
cur = stk.top();
if(cur->left ==NULL && cur->right ==NULL) {
path.push_back(cur->val);
stk.pop();
} else {
if(cur->right) {
stk.push(cur->right);
cur->right = NULL;
}
if(cur->left) {
stk.push(cur->left);
cur->left = NULL;
}
}
}
return path;
}
};
int main() {
}