-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathbinary-tree-inorder-traversal.cpp
More file actions
38 lines (38 loc) · 982 Bytes
/
binary-tree-inorder-traversal.cpp
File metadata and controls
38 lines (38 loc) · 982 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
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> v;
vector<int> inorderTraversal(TreeNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
v.clear();
if(root == NULL)
return v;
stack<TreeNode*> s;
s.push(root);
while(!s.empty()) {
while(NULL != root->left) {
root = root->left;
s.push(root);
}
while(!s.empty()) {
root = s.top();
s.pop();
v.push_back(root->val);
if(NULL != root->right) {
root = root->right;
s.push(root);
break;
}
}
}
return v;
}
};