-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmorris.cpp
30 lines (30 loc) · 1.06 KB
/
morris.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
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> nodes;
while (root) {
if (root -> left) {
// find inorder predeccsor
TreeNode* pre = root -> left;
while (pre -> right && pre -> right != root) {
pre = pre -> right;
}
// if not visited before, so rhs is null
if (!pre -> right) {
pre -> right = root;// link to curr node before
root = root -> left;// going left
} else {
// visited before, so now destroy the link
pre -> right = NULL;
nodes.push_back(root -> val);// push it
root = root -> right;// left done, self done , now go right
}
} else {
// left completed
nodes.push_back(root -> val);// push it
root = root -> right;// go right
}
}
return nodes;
}
};