美文网首页
LintCode 7-二叉树的序列化和反序列化

LintCode 7-二叉树的序列化和反序列化

作者: 胡哈哈哈 | 来源:发表于2016-05-21 16:55 被阅读1013次

分析

前序和中序唯一确定一颗二叉树

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */
class Solution {
public:
    /**
     * This method will be invoked first, you should design your own algorithm 
     * to serialize a binary tree which denote by a root node to a string which
     * can be easily deserialized by your own "deserialize" method later.
     */
    string serialize(TreeNode *root) {
        // write your code here
        vector<int> pre, in;
        inOrder(root, in);
        preOrder(root, pre);
        stringstream ss;
        for (int i = 0; i < in.size(); ++i) {
            ss << in[i];
            if (i != in.size() - 1) {
                ss << ' ';
            }
        }
        ss << '#';
        for (int i = 0; i < pre.size(); ++i) {
            ss << pre[i];
            if (i != pre.size() - 1) {
                ss << ' ';
            }
        }
        return ss.str();
    }
    
    void inOrder(TreeNode *curr, vector<int> &v) {
        if (!curr) return;
        inOrder(curr->left, v);
        v.push_back(curr->val);
        inOrder(curr->right, v);
    }
    
    void preOrder(TreeNode *curr, vector<int> &v) {
        if (!curr) return;
        v.push_back(curr->val);
        preOrder(curr->left, v);
        preOrder(curr->right, v);
    }

    /**
     * This method will be invoked second, the argument data is what exactly
     * you serialized at method "serialize", that means the data is not given by
     * system, it's given by your own serialize method. So the format of data is
     * designed by yourself, and deserialize it here as you serialize it in 
     * "serialize" method.
     */
    TreeNode *deserialize(string data) {
        // write your code here
        if (data == "#") return NULL;
        int br;
        for (br = 0; br < data.length() && data[br] != '#'; ++br);
        string front = data.substr(0, br);
        string rear = data.substr(br + 1, data.length() - br - 1);
        vector<int> in = getVector(front);
        vector<int> pre = getVector(rear);
        TreeNode *root = NULL;
        createTree(in, 0, in.size() - 1, pre, 0, pre.size() - 1, root);
        return root;
    }
    
    vector<int> getVector(string s) {
        vector<int> v;
        stringstream ss;
        ss << s;
        int count = 0;
        for (int i = 0; i < s.length(); ++i) {
            if (s[i] == ' ') ++count;
        }
        ++count;
        for (int i = 0; i < count; ++i) {
            int num;
            ss >> num;
            v.push_back(num);
        }
        return v;
    }
    
    void createTree(vector<int> &in, int ii, int ij, vector<int> &pre, int pi, int pj, TreeNode *&curr) {
        if (ii > ij && pi > pj) {
            curr = NULL;
            return;
        }
        int mid = pre[pi];
        int br;
        for (br = ii; br <= ij; ++br) {
            if (in[br] == mid) break;
        }
        curr = new TreeNode(mid);
        createTree(in, ii, br - 1, pre, pi + 1, pi + br - ii, curr->left);
        createTree(in, br + 1, ij, pre, pi + br - ii + 1, pj, curr->right);
    }
};

相关文章

  • 二叉树序列化和反序列化

    二叉树序列化和反序列化 前序 序列化和反序列化

  • 二叉树的三种遍历方法

    二叉树的序列化 为了方便构造二叉树来验证我们的算法,这里先介绍下二叉树的序列化和反序列化。 序列化 先序遍历整颗二...

  • 剑指Offer-61 二叉树序列化

    请实现两个函数,分别用来序列化和反序列化二叉树 利用广度遍历实现二叉树的序列化和非序列化。核心思想:广度遍历

  • 剑指offer刷题记录(C++版本)(之七)

    61.序列化二叉树??? 题目:请实现两个函数,分别用来序列化和反序列化二叉树 二叉树的序列化是指:把一棵二叉树按...

  • JZ-061-序列化二叉树

    序列化二叉树 题目描述 请实现两个函数,分别用来序列化和反序列化二叉树。二叉树的序列化是指:把一棵二叉树按照某种遍...

  • LeetCode:序列化二叉树

    面试题37. 序列化二叉树 请实现两个函数,分别用来序列化和反序列化二叉树。示例:你可以将以下二叉树: 序列化为 ...

  • 面试题37:序列化二叉树

    题目 实现两个函数,分别用来序列化和反序列化二叉树 解题思路 序列化根据前序遍历的顺序序列化二叉树,从根节点开始,...

  • 37:序列化二叉树

    题目37:序列化二叉树 请实现两个函数,分别用来序列化和反序列化二叉树。将树写入一个文件被称为“序列化”,读取文件...

  • 剑指offer 38- 序列化二叉树

    请实现两个函数,分别用来序列化和反序列化二叉树。 您需要确保二叉树可以序列化为字符串,并且可以将此字符串反序列化为...

  • 剑指offer - 序列化二叉树

    题目 请实现两个函数,分别序列化和反序列化二叉树。这里的序列化指的是将一棵二叉树保存到文件中,反序列化就是从文件中...

网友评论

      本文标题:LintCode 7-二叉树的序列化和反序列化

      本文链接:https://www.haomeiwen.com/subject/bhhorttx.html