2014-05-02 00:59

题目链接

原题:

Given a normal binary tree, write a function to serialize the tree into a string representation (returning the string), and also a function to deserialize a serialized string into the original binary tree.

题目:给定一棵二叉树,请设计序列化和反序列化的方法。

解法:我的方法是使用前序遍历来进行序列化,其中大括号"{}"包含了数据序列,用“#”表示空指针。反序列化的思路则相反。

代码:

 // http://www.careercup.com/question?id=5729456584916992
#include <cstdio>
#include <string>
#include <vector>
using namespace std; struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int _val = ): val(_val), left(nullptr), right(nullptr) {};
}; class BinaryTreeSerializer {
public:
string serialize(TreeNode *root) {
string res = "{"; // preorder traversal
serializeTraversal(root, res);
res[res.length() - ] = '}'; return res;
}; TreeNode *deserialize(string s) {
vector<string> data;
int i, j, len; len = (int)s.length();
i = ;
while (true) {
j = i + ;
while (s[j] != ',' && s[j] != '}') {
++j;
}
data.push_back(s.substr(i, j - i));
i = j + ;
if (i >= len) {
break;
}
} int iter = ;
TreeNode *root = nullptr; // preorder traversal
deserializeTraversal(data, root, iter); return root;
};
private:
static char ss[]; void serializeTraversal(TreeNode *root, string &res) {
if (root == nullptr) {
res += "#,";
} else {
sprintf(ss, "%d", root->val);
res += string(ss);
res.push_back(',');
serializeTraversal(root->left, res);
serializeTraversal(root->right, res);
}
}; void deserializeTraversal(vector<string> &data, TreeNode *&root, int &iter) {
++iter;
if (data[iter - ] == "#") {
root = nullptr;
} else {
int val; sscanf(data[iter - ].c_str(), "%d", &val);
root = new TreeNode(val);
deserializeTraversal(data, root->left, iter);
deserializeTraversal(data, root->right, iter);
}
};
};

Careercup - Facebook面试题 - 5729456584916992的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  3. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  4. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  5. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  6. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  7. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  8. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  9. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

随机推荐

  1. disucz!NT 3.5.0 验证码点击不能变化只是样式变化

    来博客园这么久了,第一次写博客啊!公司有个论坛10年没动了,是discuz!NT 3.5.0版本的,由于验证码不能变化老是被人刷.网上找了很多资料根本没有.可能有同行下次也会遇到这样的问题,我就把我的 ...

  2. C# Tips: Draw a data table in console

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. MSSQL数据库逻辑文件名修改与查看

    逻辑文件名是什么 你用的程序连接的时候使用的是数据库名,然后你在你的数据库右击属性的时候,左上角单击"文件",可以看到,数据库名和逻辑文件名是不一样的,你可以看自己的数据库的路径下 ...

  4. Android数据库之SQLite数据库

    Android数据库之SQLite数据库 导出查看数据库文件 在android中,为某个应用程序创建的数据库,只有它可以访问,其它应用程序是不能访问的,数据库位于Android设备/data/data ...

  5. 第六十九篇、OC_录制语音和播放语音功能的实现

    录制: 1.设置全局属性 NSURL *recordedFile;//存放路径 AVAudioPlayer *player;//播放 AVAudioRecorder *recorder;//录制 NS ...

  6. C语言知识总结(4)

    变量的作用域 C语言根据变量作用域的不同,将变量分为局部变量和全局变量 1.局部变量 1> 定义:在函数内部定义的变量,称为局部变量.形式参数也属于局部变量. 2> 作用域:局部变量只在定 ...

  7. UI4_LabelChess

    // // AppDelegate.m // UI4_LabelChess // // Created by zhangxueming on 15/6/29. // Copyright (c) 201 ...

  8. jquery的延迟加载插件Lazy Load Plugin for jQuery

    下载:https://github.com/tuupola/jquery_lazyload 使用:http://www.appelsiini.net/projects/lazyload 翻译:http ...

  9. 洛谷 P3374 【模板】树状数组 1

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. ...

  10. C++ Strings(字符串)

    Constructors 构造函数,用于字符串初始化 Operators 操作符,用于字符串比较和赋值 append() 在字符串的末尾添加文本 assign() 为字符串赋新值 at() 按给定索引 ...