-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary Tree Inorder and preorder and postorder Traversal .cpp
More file actions
118 lines (110 loc) · 3.08 KB
/
Binary Tree Inorder and preorder and postorder Traversal .cpp
File metadata and controls
118 lines (110 loc) · 3.08 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// recursion method.
class Solution {
public:
void inorder(TreeNode *root, vector<int> & result){
if(root != NULL){
inorder(root->left, result);
result.push_back(root -> val);
inorder(root -> right, result);
}
}
vector<int> inorderTraversal(TreeNode *root) {
vector<int> result;
inorder(root, result);
return result;
}
};
// iteration method
// idea: inorder: left, root, right.
// we push all the left node into the stack the most left one at the top of the stack
// then pop it and go check the right side of the node
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> result;
const TreeNode *p = root;
stack<const TreeNode *> cache;
while(p != NULL || !cache.empty()){
if( p != NULL){
cache.push(p);
p = p -> left;
}else{
p = cache.top();
cache.pop();
result.push_back(p->val);
p = p -> right;
}
}
return result;
}
};
// PREORDER TRAVERSAL
// an empty stack
// do the following steps when the stack is not empty
// push root into the stack
// push right child of popped item to the stack
// push left child of popped item to the stack
vector<int> preorderTraversal(TreeNode *root) {
vector<int> result;
TreeNode *p = new TreeNode(-1);
stack<TreeNode *> myStack;
myStack.push(root);
while(!myStack.empty()){
p = myStack.top();
result.push_back(p -> val);
myStack.pop();
if(p -> right) myStack.push(p -> right);
if( p -> left) myStack.push( p -> left);
}
return result;
}
// POSTORDER TRAVERSAL
// Two methods: 1. two stacks 2. one stack
// 1. create two stacks
// 2. push root to the first stack
// repeat the actions until first stack is empty
// 3. pop the root and push into second stack
// 4. push the left and right children of popped item into the first stack
vector<int> postorderTraversal(TreeNode *root) {
vector<int> result;
if( root == NULL) return;
stack<TreeNode *> myStack1;
stack<TreeNode *> myStack2;
myStack1.push(root);
while(!myStack1.empty()){
TreeNode *p = myStack1.top();
result.push_back(p->val);
myStack1.pop();
myStack2.push(p);
if( p -> right) myStack1.push(p -> right);
if( p -> left) myStack1.push(p -> left);
}
std::reverse(result.begin(), result.end());
return result;
}
// one stack method
// push each node twice and compare each time the top of the stack and curr value
vector postorderTraversal(TreeNode *root) {
vector<int> result;
if( root == NULL) return;
stack<TreeNode *> myStack;
myStack.push(root);
myStack.push(root);
while(!myStack.empty()){
TreeNode *curr = myStack.top();
myStack.pop();
if(!myStack.empty() && curr -> val == myStack.top() -> val){
if( curr -> right){
myStack.push( curr -> right);
myStack.push( curr -> right);
}
if(curr -> left){
myStack.push( curr -> left);
myStack.push( curr -> left);
}
}else{
result.push_back(curr -> val);
}
}
return result;
}