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. sql事务(Transaction)用法介绍及回滚实例

    sql事务(Transaction)用法介绍及回滚实例 事务(Transaction)是并发控制的单位,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位.通过事务, S ...

  2. 如何预览将要上传的图片-使用H5的FileAPI

    这篇将要说的东西已经不新鲜了. 参考资料: Reading files in JavaScript using the File APIs (鉴于作者在美国, 我姑且认为作者母语是英语, 当然链接中有 ...

  3. erlang mac os 10.9 卸载脚本

    #!/bin/bash if [ "$(id -u)" != "0" ]; then echo "Insufficient permissions. ...

  4. SpringMVC核心分发器DispatcherServlet分析[附带源码分析]

    目录 前言 DispatcherServlet初始化过程 DispatcherServlet处理请求过程 总结 参考资料 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不 ...

  5. 【新手学Python】一、基础篇

    由于以前处理数据用Matlab和C,最近要处理大量文本文件,用C写实在是太繁琐,鉴于Python的强大文本处理能力,以及其在Deep Learning上有着很大优势,本人打算从即日起学习Python, ...

  6. ajax pagination 布局刷新

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  7. java 输入年月,获取日历表

    /* 做日历作业: 输入一个时间如: 2016-11 就显示2016年11月的日历显示 */ import java.util.*; class calendar { public static vo ...

  8. Daily Scrum02 12.08

    编译大作业的第一次检查终于过去了,已经经过这次检查的组员们可以暂时松一口气了. 也希望编译大作业有着落的成员可以多花一些时间在团队任务上,帮其他的组员多分担一些工作. 第一次没来的及检查的同学,或是没 ...

  9. 获取移除指定Url参数(原创)

    /// <summary> /// 移除指定的Url参数 /// 来自:http://www.cnblogs.com/cielwater /// </summary> /// ...

  10. 如何在移动设备上调试html5开发的网页

    在我们用phoengap+html5做的移动app中,经常遇到的问题就是 本地网页兼容但是到了app出现不兼容的情况,原因是手机端页面,大多运行在webkit内核的浏览器上,但还是会因平台.厂商的不同 ...