61.序列化二叉树

题目描述

请实现两个函数,分别用来序列化和反序列化二叉树


思路一:

选用vector<int> arr作为辅助,最后转为int* 和char* 的数据类型,由于‘#’不方便表示,因此用一个不会出现的数0x23333代替。方法上是使用递归的先序遍历。


代码一:

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
    vector<int> arr;
    void dfs(TreeNode* p) {
        if(!p) arr.push_back(0x23333);  //0x23333表示一个NULL,最好是用转义字符‘#’表示
        else {
            arr.push_back(p->val);
            dfs(p->left);
            dfs(p->right);
        }
    }
    TreeNode* dfs2(int** str) {
        if(**str == 0x23333) {
            ++*str;
            return NULL;
        } else {
            TreeNode* res = new TreeNode(**str);
            ++*str;
            res->left = dfs2(str);
            res->right = dfs2(str);
            return res;
        }
    }
    char* Serialize(TreeNode *root) {
        arr.clear();
        dfs(root);
        int* res = new int[arr.size()];
        for(unsigned int i = 0; i < arr.size(); ++i) res[i] = arr[i];
        return (char*)res;
    }
    TreeNode* Deserialize(char *str) {
        TreeNode* res;
        int *p = (int*)str;
        res = dfs2(&p);
        return res;
    }
    
};

思路二:

与思路一相同,但整个过程不使用辅助数组,直接用指针实现。由于需要让char*向后移动并改变,因此形参值要设定为char**


代码二:

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
    char* Serialize(TreeNode *root) {
        if(root == NULL) return NULL;
        string str;
        Serialize(root, str);
        char *res = new char[str.size() + 1];
        int i;
        for(i = 0; i < str.size(); ++i) {
            res[i] = str[i];
        }
        res[i] = '\0';
        return res;
    }
    void Serialize(TreeNode *root, string &str) {
        if(root == NULL) {
            str += '#';
            return;
        }
        string r = to_string(root->val);  //可以是单个字符‘5’,也可能是字符串‘555’
        str += r;
        str += ',';  //用逗号将数字隔开
        Serialize(root->left, str);
        Serialize(root->right, str);
    }
    
    
    TreeNode* Deserialize(char *str) { 
        if(str == NULL) return NULL;
        TreeNode* res = Deserialize(&str);
        return res;
    }
    TreeNode* Deserialize(char **str) {
        //由于递归时,会不断的向后取字符串
        //因此,形参必须是char**类型,
        //以保证得到递归后指针str指向未被读取的字符
        if(**str == '#') {
            ++(*str);
            return NULL;
        }
        int num = 0;
        while(**str != '\0' && **str != ',') {
            num = num*10 + ((**str) - '0');
            ++(*str);
        }
        TreeNode *root = new TreeNode(num);
        if(**str == '\0') {
            return root;
        } else {
            ++(*str);
            root->left = Deserialize(str);
            root->right = Deserialize(str);
            return root;
        }
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
树的存储与遍历: 1.初始叉树 ```c++ #include <iostream> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; TreeNode* createTree() { int val; cin >> val; if (val == -1) { return NULL; } TreeNode* root = new TreeNode(val); root->left = createTree(); root->right = createTree(); return root; } ``` 2.先序遍历二叉树 ```c++ void preOrder(TreeNode* root) { if (root == NULL) { return; } cout << root->val << " "; preOrder(root->left); preOrder(root->right); } ``` 3.中序遍历二叉树 ```c++ void inOrder(TreeNode* root) { if (root == NULL) { return; } inOrder(root->left); cout << root->val << " "; inOrder(root->right); } ``` 4.后序遍历二叉树 ```c++ void postOrder(TreeNode* root) { if (root == NULL) { return; } postOrder(root->left); postOrder(root->right); cout << root->val << " "; } ``` 5.销毁二叉树 ```c++ void destroyTree(TreeNode* root) { if (root == NULL) { return; } destroyTree(root->left); destroyTree(root->right); delete root; } ``` 二叉树的复原: 1.由前序、中序序列确定复原二叉树 ```c++ TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { if (preorder.empty() || inorder.empty()) { return NULL; } int rootVal = preorder[0]; TreeNode* root = new TreeNode(rootVal); vector<int>::iterator it = find(inorder.begin(), inorder.end(), rootVal); int leftSize = it - inorder.begin(); vector<int> leftPreorder(preorder.begin() + 1, preorder.begin() + 1 + leftSize); vector<int> leftInorder(inorder.begin(), it); vector<int> rightPreorder(preorder.begin() + 1 + leftSize, preorder.end()); vector<int> rightInorder(it + 1, inorder.end()); root->left = buildTree(leftPreorder, leftInorder); root->right = buildTree(rightPreorder, rightInorder); return root; } ``` 2.由中序、后序序列确定复原二叉树 ```c++ TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { if (inorder.empty() || postorder.empty()) { return NULL; } int rootVal = postorder.back(); TreeNode* root = new TreeNode(rootVal); vector<int>::iterator it = find(inorder.begin(), inorder.end(), rootVal); int leftSize = it - inorder.begin(); vector<int> leftInorder(inorder.begin(), it); vector<int> leftPostorder(postorder.begin(), postorder.begin() + leftSize); vector<int> rightInorder(it + 1, inorder.end()); vector<int> rightPostorder(postorder.begin() + leftSize, postorder.end() - 1); root->left = buildTree(leftInorder, leftPostorder); root->right = buildTree(rightInorder, rightPostorder); return root; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值