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. dom操作中的js优化

    频繁地对于DOM进行操作的很是损耗性能,但在富网页应用中我们编写脚本无可避免地要跟DOM打交道,到底怎么才能优化这个性能瓶颈呢,大致从以下三种情况去考虑: 访问和修改DOM元素 修改DOM样式,会造成 ...

  2. C#--深入分析委托与事件

    本篇文章将为你介绍一下 Delegate 的使用方式,逐渐揭开 C# 当中事件(Event)的由来,它能使处理委托类型的过程变得更加简单. 还将为您解释委托的协变与逆变,以及如何使用 Delegate ...

  3. 可以伸缩的查询面板 (searchBar)

    最近有这样的需求,一个页面查询条件特别多,一次全部展示出来的话就占用大量的空间,所以分成了两类,简单搜索和高级搜索,当点击高级搜索的时候就会全部显示. 这样就存在一个问题,页面(navTab,dial ...

  4. python中字典dict pop方法

    首先引用下pythondoc pop(key[, default]) If key is in the dictionary, remove it and return its value, else ...

  5. win8.1开启虚拟wifi

    1. 使用管理员身份打开cmd 2. 然后输入netsh wlan set hostednetwork mode=allow 3. 接着输入netsh wlan start hostednetwork ...

  6. iOS - 网络语线程(OC)

    1. 检测网络状态 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the vi ...

  7. MySQL Limit order by

    今天写模糊查询的时候,按照时间排序并进行分页时,在mybatis的映射文件中有这样一条sql语句 SELECT <include refid="Base_Column_List&quo ...

  8. SilverLight命名空间详解-新手入门

    1.核心命名空间 1.xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"是silverlight的核 ...

  9. C++ Maps 映射

    C++ Maps是一种关联式容器,包含“关键字/值”对 begin() 返回指向map头部的迭代器 clear() 删除所有元素 count() 返回指定元素出现的次数 empty() 如果map为空 ...

  10. pancake的排序- 1.3 一摞烙饼的排序 《编程之美》读书笔记03

    问题:     星期五的晚上,一帮同事在希格玛大厦附近的“硬盘酒吧”多喝了几杯.程序员多喝了几杯之后谈什么呢?自然是算法问题.有个同事说:“我以前在餐馆打工,顾客经常点非常多的烙饼.店里的饼大小不一, ...