zoukankan      html  css  js  c++  java
  • 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

     1 package uber;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 import java.util.Stack;
     6 
     7 public class SeDeTree {
     8     public class TreeNode {
     9         int val;
    10         List<TreeNode> children;
    11         public TreeNode (int num) {
    12             this.val = num;
    13             this.children = new ArrayList<TreeNode>();
    14         }
    15     }
    16     
    17     
    18     TreeNode deserialize(String input) {
    19         if (input==null || input.length()==0) return null;
    20         char[] in = input.toCharArray();
    21         TreeNode root = new TreeNode(0); //initialize
    22         Stack<TreeNode> stack = new Stack<TreeNode>();
    23         int pos = 0;
    24         
    25         while (pos < input.length()) {
    26             if (in[pos] == '(') {
    27                 pos++;
    28                 int number = 0; // each treenode val
    29                 while (pos<input.length() && Character.isDigit(in[pos])) {
    30                     number = number*10 + (int)(in[pos] - '0');
    31                     pos++;
    32                 }
    33                 TreeNode cur = new TreeNode(number);
    34                 if (stack.isEmpty()) {
    35                     root = cur;
    36                 }
    37                 else {
    38                     stack.peek().children.add(cur);
    39                 }
    40                 stack.push(cur);
    41             }
    42             else if (in[pos] == ')') {
    43                 stack.pop();
    44                 pos++;
    45             }
    46         }
    47         return root;
    48     }
    49     
    50     
    51     String serialize(TreeNode cur) {
    52         if (cur == null) return "";
    53         StringBuilder res = new StringBuilder();
    54         res.append('(');
    55         res.append(cur.val);
    56         for (TreeNode child : cur.children) {
    57             String str = serialize(child);
    58             res.append(str);
    59         }
    60         res.append(')');
    61         return res.toString();
    62     }
    63     
    64     /**
    65      * @param args
    66      */
    67     public static void main(String[] args) {
    68         // TODO Auto-generated method stub
    69         SeDeTree sol = new SeDeTree();
    70         //String in = "(1(2)(3(5)(6))(4(7)))";
    71         String in = "(10(2(3)(4))(5)(6))";
    72         TreeNode root = sol.deserialize(in);
    73         System.out.println(root.val);
    74         String output = sol.serialize(root);
    75         System.out.println(output);
    76     }
    77 
    78 }
  • 相关阅读:
    【IT笔试面试题整理】字符串的组合
    【IT笔试面试题整理】字符串的排列
    进程与线程的区别?转
    【IT笔试面试题整理】判断一个树是否是另一个的子树
    【IT笔试面试题整理】连续子数组的最大和
    【IT笔试面试题整理】反转链表
    【IT笔试面试题整理】不用加减乘除做加法
    java Singleton 几种方式解析转
    【IT笔试面试题整理】判断链表是否存在环路,并找出回路起点
    【IT笔试面试题整理】删除无序链表中重复的节点
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6259078.html
Copyright © 2011-2022 走看看