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 ...
随机推荐
- android--asp.net webservice 返回json
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- bzoj4196 [Noi2015]软件包管理器 树链剖分+线段树
先把树剖分了(又是dfs1.dfs2),然后区间求和.区间覆盖即可 难得的1A好(shui)题 ——写了那么多题,终于有一道是1A的了,加上上一次连续交了几遍A的程序,我的状态莫名好看啊233 总结: ...
- thinkphp pathinfo nginx 无法加载模块:Index
thinkphp 报了 无法加载模块:Index 错误位置 FILE: /var/multrix/wxactivity_archive/ThinkPHP/Library/Think/Dispatche ...
- Node.js用ES6原生Promise对异步函数进行封装
Promise的概念 Promise 对象用于异步(asynchronous)计算..一个Promise对象代表着一个还未完成,但预期将来会完成的操作. Promise的几种状态: pending:初 ...
- lua元表与元方法
lua中提供的元表(metatable)与元方法(metamethod)是一种非常重要的语法,metatable主要用于做一些类似于C++重载操作符式的功能. lua中提供的元表是用于帮助lua变量完 ...
- Intellij如何设置编译后自动重新加载class文件?
前段时间突然发现Intellij不能自动重新加载类了,每次编译后都要重新启动项目,才能显示更新效果,后来网上查询Intellij下如何配置热部署,都说是要配置构件,然后在web容器的编辑页面选择upd ...
- Oracel EBS - Search Report by Response & Group
- C++的简单“五子棋”游戏,只是核心代码,资源代码未添加
ChessBoard.h #ifndef __CHESS_BOARD_H__ #define __CHESS_BOARD_H__ #include "DataStruct.h" # ...
- android中工作线程安全
当应用程序启动,创建了一个叫“main”的线程,用于管理UI相关,又叫UI线程.其他线程叫工作线程(Work Thread). Single Thread Model 一个组件的创建并不会新建一个线程 ...
- Java字符串常量池
JVM为了减少字符串对象的重复创建,维护了一个特殊的内存,这段内存被称为字符串常量池. Java中字符串对象的创建有两种形式:一种是字面量形式,String str = "a":一 ...