LeetCode 449 Serialize and Deserialize BST

LeetCode 449

Serialize and Deserialize BST

  • Problem Description:
    给出一棵二叉搜索树,将其转换成string类型,并能根据该string字符串还原成原二叉搜索树。其中,对于string字符串的输出没有限制。

    具体的题目信息:
    https://leetcode.com/problems/serialize-and-deserialize-bst/description/

  • Solution:
    • 解题思路:
      (1)通过层次遍历保存每一层结点值
      (2)递归构造二叉搜索树,特别要注意根节点位置的保存
    • 编程实现:
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Codec {
public:

    // Encodes a tree to a single string.
    //层次遍历存储二叉树所有结点的值
    string serialize(TreeNode* root) {
        string result = "";
        if (root == NULL) return result;
        TreeNode* node[15000];
        int front = -1, rear = -1, last = 0;
        vector<int> num;
        node[++rear] = root;
        while(front<rear) {
            TreeNode* p = node[++front];
            num.push_back(p->val);
            if(p->left)
                node[++rear] = p->left;
            if (p->right)
                node[++rear] = p->right;
            if (front == last) {
                last = rear;
            }
        }
        for (int i = 0; i < num.size(); i++) {
            if (i != 0) {
                result += "-";
            }
            stringstream sstr;
            sstr << num[i];
            result += sstr.str();
        }
        return result;
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        TreeNode* root = NULL;
        TreeNode* p = NULL;
        vector<int> num;
        string temp = "";
        int x;
        if (data == "") return root;
        for (int i = 0; i < data.length(); i++) {
            if (data[i] != '-') {
                temp += data[i];
                if (i == data.length()-1) {
                //将数字转换成字符串
                    stringstream pstr(temp);
                    pstr >> x;
                    num.push_back(x);
                }
            } else {
                stringstream sstr(temp);
                sstr >> x;
                num.push_back(x);
                temp = "";
            }
        }
        root = new TreeNode(num[0]);
        int j = 1, count = num.size();
        while(j < count) {
            InsertBST(root, num[j]);
            j++;
        }
        return root;
    }
    //递归插入构造二叉搜索树
    void InsertBST(TreeNode* p, int val) {
        if (val > p->val) {
            if (p->right == NULL)
                p->right = new TreeNode(val);
            else
                InsertBST(p->right, val);
        } else {
            if (val < p->val) {
                if (p->left == NULL)
                    p->left = new TreeNode(val);
                else
                    InsertBST(p->left, val);
            }
        }
    }
};

// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值