zoukankan      html  css  js  c++  java
  • 剑指offer-序列化二叉树

    序列化二叉树

    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.Queue;
    
    class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
        public TreeNode(int val) {
            this.val = val;
        }
    }
    public class Solution {
        int index = -1;//字符数组指针 
        String Serialize(TreeNode root) {     //将树序列化成字符串
            if(root == null) return "#";
            return "" + root.val + " " + Serialize(root.left) + " " + Serialize(root.right);
    //        StringBuilder sb = new StringBuilder();
    //        if(root == null) {
    //            return sb.append("#,").toString();
    //        }
    //        sb.append(root.val + ",");
    //        sb.append(Serialize(root.left));
    //        sb.append(Serialize(root.right));
    //        return sb.toString();
      }
        TreeNode Deserialize(String str) {
           index++;
           String[] strr = str.split(" ");
           if(strr[index].equals("#")) return null;
           TreeNode node = new TreeNode(Integer.valueOf(strr[index]));
           node.left = Deserialize(str);
           node.right = Deserialize(str);
           return node;
      }
        
         
        //层次打印二叉树
       public static ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
            Queue<TreeNode> queue = new LinkedList<>();
            ArrayList<Integer> ret = new ArrayList<>();
            queue.add(root);
            while(!queue.isEmpty()) {
                TreeNode t = queue.poll();
                if(t == null) continue;
                ret.add(t.val);
                queue.add(t.left);
                queue.add(t.right);
            }
            return ret;
        }
        public static void main(String[] args) {
            Solution solution = new Solution();
            TreeNode s = new TreeNode(1);
            s.left = new TreeNode(2);
            s.right = new TreeNode(3);
            s.left.left = new TreeNode(4);
            s.left.right = new TreeNode(5);
            s.right.left = new TreeNode(6);
            s.right.right = new TreeNode(7);
            String s1 = solution.Serialize(s);
            System.out.println(s1);
            /**************************************/
            String sstr = "1,2,#,5,#,#,3,6,#,#,#,";
            String str = "1 2 4 # # 5 # # 3 6 # # 7 # # ";
            TreeNode node = solution.Deserialize(str);
            System.out.println(PrintFromTopToBottom(node));
        }
    }
    /*
    t = TreeNode(8)
    t1 =TreeNode(6)
    t2 = TreeNode(10)
    t3 = TreeNode(5)
    t4 =TreeNode(7)
    t5 = TreeNode(9)
    t6 = TreeNode(11)
    t.left = t1
    t.right = t2
    t1.left = t3
    t1.right = t4
    t2.left = t5
    t2.right = t6
    print Solution().Serialize(t)
    print Solution().Deserialize(Solution().Serialize(t))
     */

    59ms

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Codec {
    public String serialize(TreeNode root) 
    {
        if(root == null) return "#";
        
        return "" + root.val + " " + serialize(root.left) + " " + serialize(root.right);
    }
    
    
    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) 
    {
        return build(new Scanner(data));
    }
    
    private TreeNode build(Scanner sc)
    {
        if(!sc.hasNext()) return null;
        String tk = sc.next();
        if(tk.equals("#")) return null;
        
        TreeNode root = new TreeNode(Integer.parseInt(tk));
        root.left = build(sc);
        root.right = build(sc);
        
        return root;
    }
    }
    
    // Your Codec object will be instantiated and called as such:
    // Codec codec = new Codec();
    // codec.deserialize(codec.serialize(root));
  • 相关阅读:
    Bzoj 3654 图样图森波 题解
    1.27号考试记录
    博弈论入门小结
    「考试总结」2020-11-18 爆零
    「补题」考试题泛做
    CSP2020 游记,总结与题解
    Luogu2827 「NOIP2016」 蚯蚓
    【学习笔记】四毛子算法
    「考试反思」2020-11-04 临行
    「考试反思」2020-10-31 警示
  • 原文地址:https://www.cnblogs.com/Roni-i/p/10345506.html
Copyright © 2011-2022 走看看