1、Morris遍历的核心思想
下面介绍莫里斯算法的核心思想:
通过递归和迭代遍历二叉树的核心是回溯,通过栈去回溯已经经过的节点,比如左子树遍历完了,回溯到根节点去遍历右子树,而莫里斯算法对于回溯的实现采用了一种特殊的办法。优点:使得空间效率从O(n)变成了O(1),时间效率依旧是O(N)。
2、moriis遍历的过程
(1)当前节点为cur(即下图中的蓝色箭头),cur来到整个树的头
1)cur无左树时,cur = cur->right;
2) cur有左树时,找到左树(即下图中的黑色箭头)上的最右节点(mostright)
a. mostright的右指针若是指向NULL:
mostright->right = cur; cur = cur->left;
b. mostright的右指针若是指向cur:
mostright->right = null; cur = cur->right;
3) 直到cur = NULL为止
(2) 对下图的遍历过程进行简单的描述
(1)初始状态:cur = root; mostright = root->left;
(2):判断有无左树, 有左树则mostright来到了左树的最右节点:最右节点(mostright->right)为NULL,则mostright->right = cur;
(3):cur = cur->left; mostright = cur->left;
(4):重复(2)
(5): cur = cur->left; mostright = cur->left(NULL);
(6): 当前节点的左树为NULL, cur = cur->right; mostright = cur->left;
(7): 判断有无左树, 有左树则mostright来到了左树的最右节点即:最右节点(mostright->right)为cur,则mostright->right = NULL;
(8): cur = cur->right, mostright = cur->left;
..........
(3) 总结
上图二叉树的morris遍历序为:① ② ④ ② ⑤ ① ③ ⑥ ③ ⑦
1)morris遍历会使得有左子树的节点,会来到2次,而递归是每个节点都会回溯3次,从而根据返回的时刻来实现二叉树的前、中、后序遍历。
3、Morris的前、中 、 后序遍历的实现
(1)morris遍历的实现
void morris(TreeNode* root){
if(root == nullptr) return nullptr;
TreeNode* cur = root;
TreeNode* mostright = nullptr;
while(cur != nullptr){
//第一步判断是否有左子树
mostright = cur->left;
if(mostright != nullptr){
//有左子树来到最右子树
while(mostright->right != nullptr && mostright->right != cur){
mostright = mostright->right;
}
//如果最右节点等于NULL
if(mostright->right == nullptr){
mostright->right = cur;
cur = cur->left;
continue;
}
else{ //如果最右节点等于cur
mostright->right = nullptr;
}
}
cur = cur->right;
}
}
(2)前序遍历的实现
vector<int> inorderTraversal(TreeNode* root) {
vector<int> number;
if(root == nullptr) return number;
TreeNode* cur = root;
TreeNode* mostright = nullptr;
while(cur != nullptr){
//第一步判断是否有左子树
mostright = cur->left;
if(mostright != nullptr){
//有左子树来到最右子树
while(mostright->right != nullptr && mostright->right != cur){
mostright = mostright->right;
}
//如果最右节点等于NULL
if(mostright->right == nullptr){
mostright->right = cur;
number.push_back(cur->val);
cur = cur->left;
continue;
}
else{ //如果最右节点等于cur
mostright->right = nullptr;
}
}
else{
number.push_back(cur->val);
}
cur = cur->right;
}
return number;
}
(3)中序遍历的实现
vector<int> inorderTraversal(TreeNode* root) {
vector<int> number;
if(root == nullptr) return number;
TreeNode* cur = root;
TreeNode* mostright = nullptr;
while(cur != nullptr){
//第一步判断是否有左子树
mostright = cur->left;
if(mostright != nullptr){
//有左子树来到最右子树
while(mostright->right != nullptr && mostright->right != cur){
mostright = mostright->right;
}
//如果最右节点等于NULL
if(mostright->right == nullptr){
mostright->right = cur;
cur = cur->left;
continue;
}
else{ //如果最右节点等于cur
mostright->right = nullptr;
}
}
number.push_back(cur->val);
cur = cur->right;
}
return number;
}
(4)后序遍历的实现(较为复杂)
1)找到可回溯两次的节点(有左子树的节点),到达该节点后,逆序打印其左树的右边界
2)最后逆序打印整个树的右边界
class Solution {
vector<int> number;
public:
vector<int> inorderTraversal(TreeNode* root) {
if(root == nullptr) return number;
TreeNode* cur = root;
TreeNode* mostright = nullptr;
while(cur != nullptr){
//第一步判断是否有左子树
mostright = cur->left;
if(mostright != nullptr){
//有左子树来到最右子树
while(mostright->right != nullptr && mostright->right != cur){
mostright = mostright->right;
}
//如果最右节点等于NULL
if(mostright->right == nullptr){
mostright->right = cur;
cur = cur->left;
continue;
}
else{ //如果最右节点等于cur
mostright->right = nullptr;
//反转其左树的右边界(链表的反转)
TreeNode* head = reverseSubTree(cur->left);
//打印
Printreserve(head);
//恢复
cur->left = reverseSubTree(head);
}
}
cur = cur->right;
}
//最后逆序整个树的右边界
TreeNode* head = reverseSubTree(root->right);
//打印
Printreserve(head);
//恢复
root->right = reverseSubTree(head);
number.push_back(root->val);
return number;
}
TreeNode* reverseSubTree(TreeNode* head){
TreeNode* pre = nullptr;
TreeNode* form = head;
TreeNode* next = form->right;
if(next == nullptr){
form->right = pre;
}
while(next != nullptr){
form->right = pre;
pre = form;
form = next;
next = form->right;
}
return form;
}
void Printreserve(TreeNode* head){
while(head != nullptr){
number.push_back(head->val);
head = head->right;
}
}
}