谷歌面经 Tree Serialization
http://www.careercup.com/question?id=4868040812396544
You should transform an structure of multiple tree from machine A to machine B. It is a serialization and deserialization problem, but i failed to solve it well.
You could assume the struct is like this:
struct Node{
string val;
vector<Node*> sons;
}
and in machine A, you will given the point to root Node, and in machine B,you should return a pointer to root Node.
class Node:
def __init__(self, val):
self.val = val
self.sons = [] class Solution:
def __init__(self, root):
self.sequence = str(root.val)
cur = [root]
while cur:
next = []
for i in cur:
self.sequence += str(len(i.sons))
for ii in i.sons:
next.append(ii)
self.sequence += str(ii.val)
cur = next def de_serial(self):
strs = self.sequence
# print strs
idx = 1
cur = [Node(int(strs[0]))]
root = cur[0]
while cur and idx < len(strs):
next = []
for i in cur:
num = int(strs[idx])
# print 'num', num
idx += 1 # notice this, idx increment not in while but in for
for j in range(num):
tmp = Node(int(strs[idx]))
# print tmp.val
i.sons.append(tmp)
next.append(tmp)
idx += 1 cur = next
# print map(lambda x: x.val, next)
return root if __name__ == "__main__":
tree = Node(1)
two = Node(2)
three = Node(3)
four = Node(4)
five = Node(5)
six = Node(6)
seven = Node(7)
tree.sons = [two, three, four]
two.sons = [five]
three.sons = [six, seven]
a = Solution(tree)
print a.de_serial()
先序遍历+中序遍历 唯一确定一棵树, 前提是树是二叉树,树的所有节点值不同
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None class Solution:
def __init__(self):
self.pre = []
self.ino = [] def preorder(self, root, list):
if not root: return
list.append(root.val)
self.preorder(root.left, list)
self.preorder(root.right, list) def inorder(self, root, lists):
if not root: return
self.inorder(root.left, lists)
lists.append(root.val)
self.inorder(root.right, lists) def serialization(self, root):
self.preorder(root, self.pre)
self.inorder(root, self.ino) def de_serial(self):
return self.de_serialization(self.pre, self.ino) def de_serialization(self, pre, inorder):
if not pre: return None
root = Node(pre[0])
i = 0
while i < len(inorder):
if pre[0] == inorder[i]:
break
i += 1 root.left = self.de_serialization(pre[1:i+1], inorder[:i])
root.right = self.de_serialization(pre[i+1:], inorder[i+1:])
return root if __name__ == "__main__":
a = Solution()
tree = Node(0)
tree.left = Node(1)
tree.right = Node(2)
a.serialization(tree)
root = a.de_serial()
print root.val, root.left.val, root.right.val
谷歌面经 Tree Serialization的更多相关文章
- [LintCode] Binary Tree Serialization
Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...
- Lintcode: Binary Tree Serialization (Serialization and Deserialization Of Binary Tree)
Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...
- override toString() function for TreeNode to output OJ's Binary Tree Serialization
class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } @Override publ ...
- [LeetCode] Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [Leetcode] Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [LeetCode] Serialize and Deserialize Binary Tree
Serialize and Deserialize Binary Tree Serialization is the process of converting a data structure or ...
- ✡ leetcode 156. Binary Tree Upside Down 旋转树 --------- java
156. Binary Tree Upside Down Add to List QuestionEditorial Solution My Submissions Total Accepted: ...
- 【LeetCode】101 - Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【LeetCode】Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
随机推荐
- 自己动手写UI库——引入ExtJs(布局)
第一: 来看一下最终的效果 第二: 来看一下使用方法: 第三: Component类代码如下所示: public class Component { pub ...
- 腾讯云CentOS 6.6安装 Nginx
一.下载Nginx 从Nginx的官网(http://nginx.org/en/download.html)下载Nginx的最新版本,这里我下载的是nginx-1.9.12. 下载完成后,得到一个如下 ...
- Origin的图片导出问题
很多会议投稿都会要求提交的pdf文件用的是type1字体,因为type1字体是矢量字体,无论怎么放大缩小都不会失真.一旦pdf里嵌入了其他非矢量字体,例如type3字体,就会通不过测试,一个典型的例子 ...
- Codeforces Round #379 (Div. 2) 总结分享
前言 初入acm的新手,打算在cf混.这几天没有比赛,就做了个最新的Virtual participation.虽然说div2比较简单,但还是被虐得体无完肤...Orz.两个小时,共6道题.最后只AC ...
- 关于C++单件模式释放对象
http://blog.csdn.net/windboyzsj/article/details/2790485 最近接触的一个项目要用到单件模式,我像往常一样哒哒(敲击键盘ing)一个单件模式的典型结 ...
- 利用同一 ASP.NET 的多个代码框架
2012 年,Microsoft 推出了两个添加到 ASP.NET 工具包的新框架:Web API 和 SignalR. 这两个框架为开发环境带来独特的开发方式,每个框架都有自身的独特之处: Web ...
- 使用Nginx负载均衡搭建高性能.NETweb应用程序二
在文章<使用Nginx负载均衡搭建高性能.NETweb应用程序一>中,让我们对Nginx有了一个初步认识,下面我们将在windows平台下面使用Nginx演示集群部署我们的web应用. 一 ...
- C#WebBrowser控件使用教程与技巧收集--苏飞收集
C#WebBrowser控件使用教程与技巧收集--苏飞收集 先来看看常用的方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System. ...
- 都昌 DCWriter电子病历编辑器演示文档截屏
- linux 解压命令
.tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!) ------------------------ ...