剑指offer突击63

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

/*
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) {
        getData(root);
        vec.push_back('\0'); //char遇到'\0'结束运行 //此句非常重要
        char *ch = &vec[0];
        return ch;
    }

    vector< char> vec;
    void getData(TreeNode *T)
    {
        if (T == NULL)
        {
            vec.push_back('#');
            return;
        }
        vec.push_back(T->val);
        getData(T->left);
        getData(T->right);
    }

    int p = -1;
    TreeNode* Deserialize(char *str) {
        if (str == NULL) return NULL;
            ++p;
        if (str[p] == '#') return NULL;
        TreeNode *T = new TreeNode(str[p]);
        T->left = Deserialize(str);
        T->right = Deserialize(str);
        return T;
    }

};
上一篇:『ACM C++』 PTA 天梯赛练习集L1 | 057-63


下一篇:Qt中关于设置styleSheet的部分总结(持续更新)