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. Windows无法启动SQL server 代理服务(服务器)错误1067:进程意外终止

    解决办法: 将关联的sql server(实例) 服务停止. 然后启动sql server代理 服务. (解决办法是从网络查询所得后,证明此法可用!)

  2. Android OpenGL ES(一)----必备知识

    1.手机的坐标空间 我们都知道要想在手机上随心所欲的绘制图形,就必须了解手机的坐标体系.下图就是将坐标映射到手机屏幕的坐标. 图1手机屏幕基本坐标系 2.OpenGL基本图形 在OpenGL里,只能绘 ...

  3. xcode5下cocos2dx横竖屏设置

    我们在开发一款游戏之前一定会考虑的一件事就是,我们的游戏是支持横屏还是竖屏,又或者是横竖屏都支持.那么如何在xcode中对项目进行设置呢?下面我就在xcode5.1.1中利用cocos2dx2.2.3 ...

  4. 【学习笔记】【C语言】结构体

    1.定义结构体变量的3种方式 1> 先定义类型,再定义变量(分开定义) struct Student {    int age; }; struct Student stu;  2> 定义 ...

  5. php面向对象的基础:创建OOP的类和字段

    类的创建 class Computer{ //类的字段(成员) //类的方法 } 对象的声明 $computer = new Computer(); new标识符是为了在内存中创建一个对象(实例),而 ...

  6. WCF之契约

    消息交换的双方,为了进行消息交换,而定义的一些数据交换规则,称之为契约. 契约只约束规则,不管实现. 契约对客户端和服务器的要求. 服务器:定义和实现契约.构建ServiceHost实例,然后暴露En ...

  7. jQuery 表单验证 jquery.validator.js

    前端开发中经常会碰到表单的制作,其中必备的功能就是提交前的一些简单的验证,非空啊.手机号码啊.E-mail等等等等,这里是一个 jQuery 的表单验证插件,蛮好用的,收录一下. 下面是验证的效果图: ...

  8. 3月3日(6) Climbing Stairs

    原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...

  9. PCB特征阻抗计算神器Polar SI9000安装及破解指南

    近年来,IC集成度的提高和应用,其信号传输频率和速度越来越高,因而在印制板导线中,信号传输(发射)高到某一定值后,便会受到印制板导线本身的影响,从而导致传 输信号的严重失真或完全丧失.这表明,PCB导 ...

  10. 一款jQuery打造的滚动条在底部滑出信息提示层

    一款jQuery打造的滚动条在底部滑出信息提示层, 当滚动鼠标滚轮,或者滚动条往下拉的时候,在右下角,弹出一个信息提示框. 有一点仿的是一个插件工具,就是网页中大家都长用到的友荐. 这款特效算一款简单 ...