-
Notifications
You must be signed in to change notification settings - Fork 243
/
BinaryTreeInorderTraversal.cpp
90 lines (70 loc) · 2.4 KB
/
BinaryTreeInorderTraversal.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// https://leetcode.com/problems/binary-tree-inorder-traversal/description/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void inorder(TreeNode* root, vector<int> &values){
if(root==NULL)
return;
inorder(root->left, values);
values.push_back(root->val);
inorder(root->right, values);
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> values;
/* Using recursion
inorder(root, values);
*/
//Morris Traversal
/*
1. Initialize current as root
2. While current is not NULL
If the current does not have left child
a) Print current’s data
b) Go to the right, i.e., current = current->right
Else
a) Find rightmost node in current left subtree OR
node whose right child == current.
If we found right child == current
a) Update the right child as NULL of that node whose right child is current
b) Print current’s data
c) Go to the right, i.e. current = current->right
Else
a) Make current as the right child of that rightmost
node we found; and
b) Go to this left child, i.e., current = current->left
*/
TreeNode* curr= root;
TreeNode* pre= NULL;
while(curr!=NULL){
if(curr->left==NULL){
values.push_back(curr->val);
curr=curr->right;
}
else{
pre= curr->left;
while(pre->right!=NULL && pre->right!=curr)
pre= pre->right;
if(pre->right==NULL){
pre->right= curr;
curr= curr->left;
}
else{
pre->right=NULL;
values.push_back(curr->val);
curr= curr-> right;
}
}
}
return values;
}
};