• 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 }
  • 相关阅读:
    Shell编程------函数应用
    Shell编程------循环语句
    Shell编程------判断语句
    Shell编程------变量、赋值和运算
    Action实现prepareable接口后定义前置方法
    动态代理模式
    hibernate持久化对象,
    view视图总结
    servlet和Struts2的线程安全性对比
    Action获取请求参数的3中方式
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6259078.html
Copyright © 2020-2023  润新知