谷歌面经 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 ...
随机推荐
- [WinAPI] API 12 [获取程序所在的目录、程序模块路径,获取和设置当前目录]
Windows系统提供一组API实现对程序运行时相关目录的获取和设置.用户可以使用GetCurrentDirectory和SetCurrentDirectory获取程序的当前目录,获取模块的路径使用G ...
- [stm32] 利用uC-BmpCvt软件生成uc-gui可调用的bmp图片
>_<:学习贴图[bmp图]:首先找一张bmp格式的图片,然后下载uC-BmpCvt软件,打开改图片 >_<:然后点击Image-Conver-Into-Best Palatt ...
- 轻量级实用JQuery表单验证插件:validateForm5
表单验证是Web项目一个必不可少的环节,而且是一项重复的劳动,于是小菜封装了一款简单的表单验证插件,名字叫:validateForm5. validateForm5插件基于Jquery,并向HTML5 ...
- 老生常谈JavaScript闭包
闭包就是指一个有权访问另外一个函数作用域中的变量的函数.--<JavaScript高级程序第三版> 本人对于闭包初次的认识就来自<高三>,首先仅仅通过“有权”’两个字我们便可以 ...
- 【原】在windows下使用VirtualEnv
VirtualEnv可以方便的解决不同项目中对类库的依赖问题.这通常是通过以下方式实现的:首先将常用的类库安装在系统环境中:然后为每个项目安装独立的类库环境.这样子可以保证每个项目都运行在独立的类库环 ...
- T4模板TextTemplatingFileGenerator和TextTemplatingFilePreprocessor区别
最近做一个项目,需要根据数据库表生成对应的实体类,于是想到了代码生成器.查阅了Nvelocity.T4.RazorEngine,对于一个微软技术派,觉得还是T4最亲切,使用简单,功能强大. 在尝试使用 ...
- LLVM和GCC的区别
最近在Mac OS X Mountain Lion下用Xcode进行开发,发现在编译选项里有如下所示的这两种编译器:一个是Apple LLVM compiler 4.2,另外一个是LLVM GCC 4 ...
- jquery时间倒计时
代码: js: function countDown(time, id) { //time的格式yyyy/MM/dd hh:mm:ss var day_elem = $(id).find('. ...
- javaweb学习总结(六)——Servlet开发(二)
一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些 ...
- spring源码 — 一、IoC容器初始化
IoC容器初始化 注意:本次的spring源码是基于3.1.1.release版本 容器:具有获取Bean功能--这是最基本功能,也是BeanFactory接口定义的主要行为,在添加了对于资源的支持之 ...