2014-05-12 07:17

题目链接

原题:

Given below is a tree/trie
A
B c D
e F
a<b<e<>>c<>d<f<>>>
above string represents the following trie/tree (visualize) and assume that there exisits a serialize method that performs above.
Now, write a deserialize method so that above string to an object model of the following
TreeNode
TreeNode[] children

题目:给定以上的字典树序列化方法,请写出相应的反序列化方法。

解法:观察序列化的字符串,可以发现序列化的规则是“字母<若干个子树>”。用一个栈就可以写出比较简洁的反序列化代码。

代码:

 // http://www.careercup.com/question?id=5799446021406720
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std; struct TrieNode {
char ch;
vector<TrieNode *> child;
TrieNode(char _ch = ): ch(_ch), child(vector<TrieNode *>()) {};
}; TrieNode *deserializeFromString(const string &str)
{
TrieNode *root;
TrieNode *ptr;
stack<TrieNode *> st; int i, n; root = nullptr;
n = (int)str.length();
i = ;
while (i < n) {
if (str[i] == '>') {
st.pop();
++i;
} else {
ptr = new TrieNode(str[i]);
i += ;
if (st.empty()) {
root = ptr;
} else {
(st.top()->child).push_back(ptr);
}
st.push(ptr);
}
} return root;
} void preorderTraversal(TrieNode *root)
{
if (root == nullptr) {
return;
}
cout << root->ch << ' ';
for (int i = ; i < (int)root->child.size(); ++i) {
preorderTraversal(root->child[i]);
}
} int main()
{
TrieNode *root = nullptr;
string str; while (cin >> str) {
root = deserializeFromString(str);
preorderTraversal(root);
cout << endl;
} return ;
}

Careercup - Microsoft面试题 - 5799446021406720的更多相关文章

  1. Careercup - Microsoft面试题 - 6314866323226624

    2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...

  2. Careercup - Microsoft面试题 - 6366101810184192

    2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...

  3. Careercup - Microsoft面试题 - 24308662

    2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...

  4. Careercup - Microsoft面试题 - 5700293077499904

    2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...

  5. Careercup - Microsoft面试题 - 5204967652589568

    2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...

  6. Careercup - Microsoft面试题 - 5175246478901248

    2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...

  7. Careercup - Microsoft面试题 - 5718181884723200

    2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...

  8. Careercup - Microsoft面试题 - 5173689888800768

    2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...

  9. Careercup - Microsoft面试题 - 6282862240202752

    2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...

随机推荐

  1. 【Android开发笔记】生命周期研究

    启动 onCreate onStart onResume 退出键 onPause onStop onDestroy 锁屏 & 按住 home键 & 被其他Activity覆盖(Sing ...

  2. Paoding-Rose学习

    * HttpServletRequest.getContextPath 获取web程序root.如果是默认位置,返回””空串,否则返回 /根路径名 * rose是如何扫描到资源的 利用spring提供 ...

  3. LeetCode Reverse Words in a String 将串中的字翻转

    class Solution { public: void reverseWords(string &s) { string end="",tem="" ...

  4. linux 命令——46 vmstat(转)

    vmstat 是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存.进程.CPU活动进行监控.他是对系统的整体情况进行统计,不足之处是无法对某个进程进行 ...

  5. json字符串转换成对象需要注意的问题

    json转换成对象的时候应该尽量避免出现特殊的符号,如“\”这样的字符在转义成数组的时候会被去除掉,最好的例子就是后台返回的内容为存储路径的JSON,这时候最好是把一个斜杠变为两个斜杠,如: [{&q ...

  6. Aizu 0525 Osenbei(状压+贪心)

    题意:翻煎饼,只能横着翻或者竖着翻.问最多有多少朝上? 行只有10,所以枚举一下2^10的状态,每列取0或1中最大的一个. 在枚举外面把饼翻好,枚举里面指针指一下就好.(位运算或bitset乱搞 #i ...

  7. iOS 不同的崩溃类型

    http://m.blog.csdn.net/kangguang/article/details/62501490 用汇编语言编写的软件跟用脚本或标记语言编写的Web应用的差别在于,前者在出现问题时会 ...

  8. 黑箱中的 retain 和 release

    https://github.com/Draveness/Analyze/blob/master/contents/objc/黑箱中的%20retain%20和%20release.md 写在前面 在 ...

  9. 解决linux系统CentOS下调整home和根分区大小《转》

    转自http://www.php114.net/2013/1019/637.html 目标:将VolGroup-lv_home缩小到20G,并将剩余的空间添加给VolGroup-lv_root   1 ...

  10. 【luogu P3608 [USACO17JAN]Balanced Photo平衡的照片】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3608 乍一看很容易想到O(N^2)的暴力. 对于每个H[i]从i~i-1找L[i]再从i+1~n找R[i], ...