-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetInOrderTraversal.cpp
37 lines (37 loc) · 998 Bytes
/
getInOrderTraversal.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
#include <bits/stdc++.h>
/*
Following is Binary Tree Node structure:
class TreeNode
{
public:
int data;
TreeNode *left, *right;
TreeNode() : data(0), left(NULL), right(NULL) {}
TreeNode(int x) : data(x), left(NULL), right(NULL) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : data(x), left(left), right(right) {}
};
*/
vector<int> getInOrderTraversal(TreeNode *root)
{
TreeNode* cur = root;
vector<int> ans;
while(cur) {
if(cur->left) {
TreeNode* t = cur->left;
while(t->right && t->right != cur)
t = t->right;
if(t->right == NULL) {
t->right = cur;
cur = cur->left;
}else {
t->right = NULL;
ans.push_back(cur->data);
cur = cur->right;
}
}else {
ans.push_back(cur->data);
cur = cur->right;
}
}
return ans;
}