U家面试prepare: Serialize and Deserialize Tree With Uncertain Children Nodes
Like Leetcode 297, Serialize and Deserialize Binary Tree, the only difference, this is not a binary tree.
The method to serialize is like this: (1(2)(3(5)(6))(4(7)))
if '(', right shift 1 position to the start of the number, use while loop to find the end of the number(either end with'(' or ')'), create a treeNode use this number as value, here we have two cases:
1. if stack is empty, this treeNode is root. push this node to stack
2. not empty, then this node is one child of stack.peek(), add this node to stack.peek().children, push this node to stack
else if ')', pop
package uber; import java.util.ArrayList;
import java.util.List;
import java.util.Stack; public class SeDeTree {
public class TreeNode {
int val;
List<TreeNode> children;
public TreeNode (int num) {
this.val = num;
this.children = new ArrayList<TreeNode>();
}
} TreeNode deserialize(String input) {
if (input==null || input.length()==0) return null;
char[] in = input.toCharArray();
TreeNode root = new TreeNode(0); //initialize
Stack<TreeNode> stack = new Stack<TreeNode>();
int pos = 0; while (pos < input.length()) {
if (in[pos] == '(') {
pos++;
int number = 0; // each treenode val
while (pos<input.length() && Character.isDigit(in[pos])) {
number = number*10 + (int)(in[pos] - '0');
pos++;
}
TreeNode cur = new TreeNode(number);
if (stack.isEmpty()) {
root = cur;
}
else {
stack.peek().children.add(cur);
}
stack.push(cur);
}
else if (in[pos] == ')') {
stack.pop();
pos++;
}
}
return root;
} String serialize(TreeNode cur) {
if (cur == null) return "";
StringBuilder res = new StringBuilder();
res.append('(');
res.append(cur.val);
for (TreeNode child : cur.children) {
String str = serialize(child);
res.append(str);
}
res.append(')');
return res.toString();
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SeDeTree sol = new SeDeTree();
//String in = "(1(2)(3(5)(6))(4(7)))";
String in = "(10(2(3)(4))(5)(6))";
TreeNode root = sol.deserialize(in);
System.out.println(root.val);
String output = sol.serialize(root);
System.out.println(output);
} }
U家面试prepare: Serialize and Deserialize Tree With Uncertain Children Nodes的更多相关文章
- 7 Serialize and Deserialize Binary Tree 序列化及反序列化二叉树
原题网址:http://www.lintcode.com/zh-cn/problem/serialize-and-deserialize-binary-tree/# 设计一个算法,并编写代码来序列化和 ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Serialize and Deserialize Binary Tree
Serialize and Deserialize Binary Tree Serialization is the process of converting a data structure or ...
- LeetCode——Serialize and Deserialize Binary Tree
Description: Serialization is the process of converting a data structure or object into a sequence o ...
- Serialize and Deserialize Binary Tree
Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...
- 449. Serialize and Deserialize BST——几乎所有树的面试题目都会回到BFS或者DFS,使用BFS,None节点存#
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 297. Serialize and Deserialize Binary Tree
题目: Serialization is the process of converting a data structure or object into a sequence of bits so ...
- LeetCode OJ 297. Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [Java]LeetCode297. 二叉树的序列化与反序列化 | Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
随机推荐
- oracle存储过程实现根据已有数据批量更新另一批数据
declare CURSOR l_c IS select col1,col2 from table1; Begin FOR i IN l_c LOOP dbms_output.put_line(i.c ...
- Skype无法收发组消息
我用微软账户登录的Skype 发现无法收发组消息 - 提示发送消息不可用 卸了重装 - 提示 "无法发送消息, 请尝试获取最新的消息版本, 或者是组内成员使用旧版本无法同时视频和发送 ...
- csipsimple 出现单通情况
今天在测试voip电话时,突然打不通了和windows端也不通,boss发怒了. 经过排查,发现设置G729编码 //设置G729编码 prefs.setCodecPriority("g72 ...
- Tomcat启动后,从spring容器中获取Bean和ServletContext
public static Object getBean(String beanName){ ApplicationContext context = ContextLoader.getCurrent ...
- POST在发送数据的时候使用的是HTTP命令
防止SQL注入 SQL(结构化查询语言)是基于美国国家标准学会(ANSI)标准,并作为共同的语言与数据库通信.每个数据库系统增加了一些专有功能到基本的ANSI SQL. SQL注入是一门将制作好的SQ ...
- 机器学习常用Python扩展包
在Ubuntu下安装Python模块通常有3种方法:1)使用apt-get:2)使用pip命令(推荐);3)easy_instal 可安装方法参考:[转]linux和windows下安装python集 ...
- 【PC网站前端架构探讨系列】关于中小型PC网站前端架构方案的讨论与实践
目 录 1.遇到的问题 2.目标 3.探讨 4.架构设想 5.流程 6.初步实现 7.存在问题 8.最后 遇到的问题 我在这个系列上篇文章 已经讲解并开始逐步应用模块化思想,不知大家还记不记得,题 ...
- JDK环境变量设置
1,新建JAVA_HOME变量 . E:\TOOLS\JAVA\JDK1.8.0_111 2,新建CLASSPATH变量 . .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\to ...
- 经典的javascript函数实例,css的. #区别
先贴javascript经典例子代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...
- 总结-Intellij Idea (快捷键 配置修改)
忽略大小写 输入sensitive,选择Code Completion,右边第一个下拉框,选择noneEditor 鼠标悬浮show quick docEditor Editor Tabs : Mar ...