-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsolution094.cpp
More file actions
64 lines (56 loc) · 1.01 KB
/
solution094.cpp
File metadata and controls
64 lines (56 loc) · 1.01 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
/**
* Binary Tree Inorder Traversal
*
* cpselvis([email protected])
* September 9th, 2016
*/
#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> inorderTraversal(TreeNode* root) {
vector<int> ret;
if (root == NULL)
{
return ret;
}
stack<TreeNode *> st;
TreeNode *pnode = root;
while (pnode != NULL || !st.empty())
{
if (pnode != NULL)
{
st.push(pnode);
pnode = pnode -> left;
}
else
{
pnode = st.top();
st.pop();
ret.push_back(pnode -> val);
pnode = pnode -> right;
}
}
return ret;
}
};
int main(int argc, char **argv)
{
TreeNode *root = new TreeNode(1);
root -> right = new TreeNode(2);
root -> right -> left = new TreeNode(3);
Solution s;
vector<int> vec = s.inorderTraversal(root);
for (auto i : vec)
{
cout << i << endl;
}
}