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

  1. 7 Serialize and Deserialize Binary Tree 序列化及反序列化二叉树

    原题网址:http://www.lintcode.com/zh-cn/problem/serialize-and-deserialize-binary-tree/# 设计一个算法,并编写代码来序列化和 ...

  2. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  3. [LeetCode] Serialize and Deserialize Binary Tree

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

  4. LeetCode——Serialize and Deserialize Binary Tree

    Description: Serialization is the process of converting a data structure or object into a sequence o ...

  5. Serialize and Deserialize Binary Tree

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

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

  7. 297. Serialize and Deserialize Binary Tree

    题目: Serialization is the process of converting a data structure or object into a sequence of bits so ...

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

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

随机推荐

  1. Hibernate Open Session In View模式【转】

    来源:http://www.yybean.com/opensessioninviewfilter-role-and-configuration 一.作用 Spring为我们解决Hibernate的Se ...

  2. wpf telerik中的book控件

    下载 telerik中的书本控件,仅供学习使用.

  3. Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  4. [转]passport.js学习笔记

    概述 passport.js是Nodejs中的一个做登录验证的中间件,极其灵活和模块化,并且可与Express.Sails等Web框架无缝集成.Passport功能单一,即只能做登录验证,但非常强大, ...

  5. Skype无法收发组消息

    我用微软账户登录的Skype 发现无法收发组消息  -  提示发送消息不可用 卸了重装  -  提示 "无法发送消息, 请尝试获取最新的消息版本, 或者是组内成员使用旧版本无法同时视频和发送 ...

  6. zk textbox 更改字体大小及高度

    .z-textbox{ height:100px; font-size:30px; padding:20px; } <textbox/> 效果如下:

  7. WPF整理-使用逻辑资源

    "Traditional application resources consist of binary chunks of data, typically representing thi ...

  8. alternatives命令用法

    alternatives是Linux下的一个功能强大的命令.只能在根权限下执行.如系统中有几个命令功能十分类似,却又不能随意删除,那么可以用替代指定一个全局的设置.alternatives常用于同一个 ...

  9. 常用数据库的驱动程序及JDBC URL:

    Oracle数据库: 驱动程序包名:ojdbc14.jar 驱动类的名字:oracle.jdbc.driver.OracleDriver JDBC URL:jdbc:oracle:thin:@dbip ...

  10. 关于CAShapeLayer的一些实用案例和技巧【转】

    本文授权转载,作者:@景铭巴巴 一.使用CAShapeLayer实现复杂的View的遮罩效果 1.1.案例演示 最近在整理一个聊天的项目的时候,发送图片的时候,会有一个三角的指向效果,指向这张图片的发 ...