积少成多

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

=====

笔记另写:

======

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
    }

 

posted on 2016-06-26 21:16  x7b5g  阅读(484)  评论(0编辑  收藏  举报