-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0094_Binary_Tree_Inorder_Traversal.cpp
82 lines (77 loc) · 1.92 KB
/
0094_Binary_Tree_Inorder_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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode* construct(vector<int> input){
if(input.size() == 0) return NULL;
TreeNode* head = new TreeNode(input[0]);
queue<TreeNode*> que;
que.push(head);
int n = input.size();
for(int i = 1; i < n; i = i + 2){
TreeNode* p = que.front();
p->left = new TreeNode(input[i]);
que.push(p->left);
if(i + 1 < n){
p->right = new TreeNode(input[i + 1]);
que.push(p->right);
}
que.pop();
}
return head;
}
class Solution {
public:
void collect(TreeNode* root, vector<int>& res){
if(!root) return;
collect(root->left, res);
res.push_back(root->val);
collect(root->right, res);
}
// recursive solution
vector<int> inorderTraversal1(TreeNode* root) {
vector<int> res;
collect(root, res);
return res;
}
// no recursive solution
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> s;
if(!root) return res;
TreeNode* p = root;
TreeNode* q;
while(p){
s.push(p);
p = p->left;
}
while(!s.empty()){
p = s.top();
res.push_back(p->val);
s.pop();
if(p->right){
q = p->right;
while(q){
s.push(q);
q = q->left;
}
}
}
return res;
}
};
int main(){
vector<int> input = {1, 2, 3, 4, 5, 6, 7, 8};
TreeNode* head = construct(input);
Solution solve;
vector<int> res = solve.inorderTraversal(head);
for(int i = 0; i < res.size(); i++) std::cout << res[i] << " ";
return 0;
}