Using Morris Traversal


#1

Here is my code:
vector Solution::inorderTraversal(TreeNode* A) {
vector B;
TreeNode* current = A;
TreeNode* pre;

while(current) {
if(current->left) {
pre = current->left;
while(pre->right!=NULL && pre->right!= current) {
pre = pre->right;
}
if(pre->right ==NULL) {
pre->right = current;
current = current->left;
}
else {
pre->right = NULL;
B.push_back(current->val);
current = current->right;
}
}
else {
B.push_back(current->val);
current = current->right;
}
}
return B;
}