• serialize-and-deserialize-bst


    https://leetcode.com/problems/serialize-and-deserialize-bst/

    1. 用到Java Queue接口,

    // LinkedList实现了Queue接口, ArrayList没有实现

    2. 用的Java String.split 函数得到 String数组。

    3. 另外一个bug是因为String比较用的 == ,没有用 equals

    package com.company;
    
    import apple.laf.JRSUIUtils;
    
    import java.util.*;
    
    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
    
    class Codec {
        // Encodes a tree to a single string.
        public String serialize(TreeNode root) {
            StringBuilder sb = new StringBuilder();
            if (root == null) {
                return "";
            }
    
            // LinkedList实现了Queue接口, ArrayList没有实现
            Queue<TreeNode> qe= new LinkedList<>();
            qe.offer(root);
            sb.append(root.val+",");
            while (!qe.isEmpty()) {
                TreeNode tn = qe.poll();
                if (tn.left != null) {
                    sb.append(tn.left.val+",");
                    qe.offer(tn.left);
                }
                else {
                    sb.append(",");
                }
                if (tn.right != null) {
                    sb.append(tn.right.val+",");
                    qe.offer(tn.right);
                }
                else {
                    sb.append(",");
                }
            }
            return sb.toString();
        }
    
        // Decodes your encoded data to tree.
        public TreeNode deserialize(String data) {
            if (data.equals("")) {
                return null;
            }
    
            String[] strs = data.split(",");
            Queue<TreeNode> qe = new LinkedList<>();
    
            if (strs.length < 1 || strs[0].equals("")) {
                return null;
            }
    
            TreeNode root = new TreeNode(Integer.valueOf(strs[0]));
            qe.offer(root);
            int i = 1;
            while (!qe.isEmpty()) {
                TreeNode tn = qe.poll();
    
                if (strs.length > i && !strs[i].equals("")) {
                    TreeNode left = new TreeNode(Integer.valueOf(strs[i]));
                    tn.left = left;
                    qe.offer(left);
                }
                i++;
                if (strs.length > i && !strs[i].equals("")) {
                    TreeNode right = new TreeNode(Integer.valueOf(strs[i]));
                    tn.right = right;
                    qe.offer(right);
                }
                i++;
            }
            return root;
        }
    }
    
    public class Main {
    
        public static void main(String[] args) throws InterruptedException {
    
            System.out.println("Hello!");
            //Solution solution = new Solution();
    
            // Your Codec object will be instantiated and called as such:
            TreeNode tn = new TreeNode(2);
            TreeNode tn1 = new TreeNode(1);
            //TreeNode tn2 = new TreeNode(3);
            tn.left = tn1;
            //tn.right = tn2;
            Codec codec = new Codec();
            String code = codec.serialize(tn);
            System.out.printf("code:%s
    ", code);
            TreeNode ret = codec.deserialize(code);
            System.out.printf("root:%d
    ", ret.val);
    
            System.out.println();
    
        }
    }
  • 相关阅读:
    技巧和诀窍;在VS 2005里优化ASP.NET 2.0Web项目的Build性能
    [资源]Flex 中文帮助
    13 Awesome Javascript CSS Menus
    兼容FF\IE的事件获得方法
    MSVCR80D.dll not found解决方案
    [转]一种革命性的自绘菜单实现
    [ZZ]马化腾关于产品设计与用户体验的培训
    [ZZ]WatiN:在.NET中测试Web应用程序
    How to Recommender Systems?
    淘宝第三届D2在上海举办
  • 原文地址:https://www.cnblogs.com/charlesblc/p/6019644.html
Copyright © 2020-2023  润新知