-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinorderTraversal.cpp
51 lines (44 loc) · 1.3 KB
/
inorderTraversal.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
/********************************
Copyright : CNIC
Author ; LiuYao
Date : 2017-8-23
Description : inorder traversal without recursive
********************************/
#include <vector>
#include <stack>
using std::vector;
using std::stack;
//Tree Node struct
struct TreeNode {
int val; //node value
TreeNode *left; //left child pointer
TreeNode *right; //right child pointer
TreeNode(int x) : val(x), left(NULL), right(NULL) {} //construct
};
//Summary : inorder traversal
//Parameters :
// root : root node
//Return : inorder traversal vector
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode *> s;
//initail the stack
while(root != NULL) {
s.push(root);
root = root -> left;
}
//while the stack is not empty, pop the stack top to visit
while(!s.empty()){
TreeNode* top = s.top();
s.pop();
res.push_back(top -> val);
//when popping the top element, add the right child of the top element
//and add the left child of the right child of top element until the left child is null.
TreeNode* cur = top -> right;
while(cur != NULL){
s.push(cur);
cur = cur -> left;
}
}
return res;
}