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的更多相关文章

  1. [LintCode] Binary Tree Serialization

    Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...

  2. 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 ...

  3. 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 ...

  4. [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 ...

  5. [Leetcode] Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  6. [LeetCode] Serialize and Deserialize Binary Tree

    Serialize and Deserialize Binary Tree Serialization is the process of converting a data structure or ...

  7. ✡ leetcode 156. Binary Tree Upside Down 旋转树 --------- java

    156. Binary Tree Upside Down Add to List QuestionEditorial Solution My Submissions   Total Accepted: ...

  8. 【LeetCode】101 - Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  9. 【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 ...

随机推荐

  1. 图书馆管理系统UML建模

    一.    业务描述 1.借阅者:借书.还书 2.图书馆管理员:书籍借出处理   书籍归还处理书籍预定 3.系统管理员:增加书目.删除或更新书目.预定信息处理.增加书籍减少书籍.增加借阅者账户信息.删 ...

  2. Linux:文件解压与压缩

    文件打包与压缩 常见压缩文件格式: |文件后缀名 |说明| |.zip |zip程序打包压缩的文件| |.rar |rar程序压缩的文件| |.7z |7zip程序压缩的文件| |.tar |tar程 ...

  3. Spring AOP(配置文件方式)

    spring配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...

  4. chosen PersistenceUnitInfo does not specify a provider class name

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceI ...

  5. 【Android】应用程序Activity启动过程分析

    在Android系统中,有两种操作会引发Activity的启动,一种用户点击应用程序图标时,Launcher会为我们启动应用程序的主Activity:应用程序的默认Activity启动起来后,它又可以 ...

  6. Atitit org.eclipse.jdt 的ast 架构 Eclipse JDT API spec

    Atitit org.eclipse.jdt 的ast 架构 Eclipse JDT API spec 继承树1 Expression的子类1 获取子类2 继承树 Astnode>express ...

  7. paip.输入法英文词库的处理 python 代码 o4

    paip.输入法英文词库的处理 python 代码 o4 目标是eng>>>中文>>atian 当输入非atian词的时候儿,能打印出 atian pinyin > ...

  8. 分析system_call中断处理过程

    分析system_call中断处理过程 上周我们使用gcc内嵌汇编调用系统调用,这次我们具体分析下过程. 将getpid嵌入menuos 代码从github下载,步骤如下: 1. 增加一个函数,get ...

  9. WizardDialog 进度条使用记录

    开发RCP的朋友们经常会使用到导航窗口, 先简单介绍一下wizardDialog,基本上他的使用方法是这样的 首先有一个WizardDialog,在dialog里面需要放一个Wizard来控制页面Wi ...

  10. MongoDB学习笔记——索引管理

    索引 索引能够提升查询的效率.没有索引,MongoDB必须扫描集合中的所有文档,才能找到匹配查询语句的文档. 索引是一种特殊的数据结构,将一小块数据集保存为容易遍历的形式.索引能够存储某种特殊字段或字 ...