leetcode449. Serialize and Deserialize BST

題目要求

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.

Design an algorithm to serialize and deserialize a binary search tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary search tree can be serialized to a string and this string can be deserialized to the original tree structure.

The encoded string should be as compact as possible.

Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.

將二叉搜索樹序列化和反序列化,序列化是指將樹用字符串的形式表示,反序列化是指將字符串形式的樹還原成原來的樣子。java

思路和代碼

對於樹的序列化,能夠直接聯想到對樹的遍歷。樹的遍歷包括前序遍歷,中序遍歷,後序遍歷和水平遍歷,而且可知前序遍歷和中序遍歷,或中序遍歷和後序遍歷能夠構成一棵惟一的樹。除此之外,由於這是一棵二叉搜索樹,可知該樹的中序遍歷就是全部元素的從小到大的排列。算法

舉個例子,假如一棵樹的結構以下:app

3
 / \
2   4
 \
  1

該樹的前序遍歷結果爲3,2,1,4,中序遍歷爲1,2,3,4。再仔細分析前序遍歷的結果,結合二叉搜索樹可知,比中間節點小的值必定位於左子樹,反之必定位於右子樹,便可以對前序遍歷進行分割3,|2,1,|4。也就是說,咱們能夠只利用前序遍歷,就能夠區分出二叉搜索樹的左子樹和右子樹。
代碼以下:less

public String serialize(TreeNode root) {
        StringBuilder sb = new StringBuilder();
        preorder(root, sb);
        return sb.toString();
    }

    public void preorder(TreeNode root, StringBuilder result) {
        if(root != null) {
            result.append(root.val);
            result.append(":");
            preorder(root.left, result);
            preorder(root.right, result);
        }
    }
    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        if(data==null || data.isEmpty()) return null;
        String[] preorder = data.split(":");
        String[] inorder = Arrays.copyOf(preorder, preorder.length);
        Arrays.sort(inorder, new Comparator<String>(){

            @Override
            public int compare(String o1, String o2) {
                Integer i1 = Integer.valueOf(o1);
                Integer i2 = Integer.valueOf(o2);
                return i1.compareTo(i2);
            }
            
        });
        
        return build(inorder, preorder, 0, 0, inorder.length);
    }
    
    public TreeNode build(String[] inorder, String[] preorder, int inorderStart, int preorderStart, int length) {
        if(length <= 0) return null;
        TreeNode root = new TreeNode(Integer.valueOf(preorder[preorderStart]));
        for(int i = inorderStart ; i < inorderStart+length ; i++) {
            if(inorder[i].equals(preorder[preorderStart])) {
                root.left = build(inorder, preorder, inorderStart, preorderStart+1, i-inorderStart);
                root.right = build(inorder, preorder, i+1, preorderStart+i-inorderStart+1, inorderStart+length-i-1);
                break;
            }
        }
        
        return root;
    }

這裏的代碼是直接使用排序生成了二叉搜索樹的中序遍歷的結果,並利用先序遍歷和中序遍歷構造了一棵二叉搜索樹。假如二叉搜索樹的節點較多,該算法將會佔用大量的額外空間。能夠只用先序遍歷做爲構造樹的輸入,代碼以下:ide

public TreeNode deserialize(String data) {
        if (data==null) return null;
        String[] strs = data.split(":");
        Queue<Integer> q = new LinkedList<>();
        for (String e : strs) {
            q.offer(Integer.parseInt(e));
        }
        return getNode(q);
    }
    
    private TreeNode getNode(Queue<Integer> q) {
        if (q.isEmpty()) return null;
        TreeNode root = new TreeNode(q.poll());//root (5)
        Queue<Integer> samllerQueue = new LinkedList<>();
        while (!q.isEmpty() && q.peek() < root.val) {
            samllerQueue.offer(q.poll());
        }
        root.left = getNode(samllerQueue);
        root.right = getNode(q);
        return root;
    }
相關文章
相關標籤/搜索