=====
笔记另写:
======
code:
class A{ public: ///inorderMorrisTraversal void inorderMorrisTraversal(TreeNode *root){ TreeNode *curr = root; TreeNode *prev = nullptr; while(curr!=nullptr){ if(curr->left == nullptr){ cout<<curr->val<<" "; curr = curr->right; }else{ ///search predecessor prev = curr->left; while(prev->right !=nullptr && prev->right!=curr){ prev = prev->right; } if(prev->right==nullptr){ prev->right = curr; curr = curr->left; }else{ prev->right = nullptr;///recover the tree cout<<curr->val<<" "; curr = curr->right; } }///if-else }///while } };
=== 前序线索化
/// void preMorris(TreeNode* root){ TreeNode *curr = root; TreeNode *prev = nullptr; while(curr!=nullptr){ if(curr->left==nullptr){ cout<<curr->val<<" "; curr = curr->right; }else{ prev = curr->left; while(prev->right!=nullptr && prev->right!=curr){ prev = prev->right; } if(prev->right==nullptr){ prev->right = curr; cout<<curr->val<<" "; curr = curr->left; }else{ prev->right = curr; curr = curr->right; } } }///while }