国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

Serialize and Deserialize Binary Tree & BST

eccozhou / 3260人閱讀

摘要:思路理論上說所有遍歷的方法都可以。但是為了使和的過程都盡量最簡單,是不錯的選擇。用作為分隔符,來表示。復(fù)雜度代碼思路這道題和之前不同,一般的樹變成了,而且要求是。還是可以用,還是需要分隔符,但是就不需要保存了。

297. Serialize and Deserialize Binary Tree

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 tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
For example, you may serialize the following tree

    1    
   /   
  2   3
     / 
    4   5

as "[1,2,3,null,null,4,5]", just the same as how LeetCode OJ serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.

思路

理論上說所有遍歷的方法都可以。但是為了使serialize和deserialize的過程都盡量最簡單,preorder是不錯的選擇。serialize的話,dfs比較好寫,deserialize的話preorder和bfs比較好寫。用“,”作為分隔符,“#”來表示null。例子里serialize之后結(jié)果就變成"1,2,3,#,#,4,5"。deserialize的時候用一個queue來保存string。

復(fù)雜度

Time: O(N), Space: O(N)

代碼
    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        // base case
        if(root == null) return "";
        StringBuilder encoded = new StringBuilder();
        encode(root, encoded);
        return encoded.substring(1).toString();
    }
    
    private void encode(TreeNode root, StringBuilder sb) {
        if(root == null) {
            sb.append(",#");
            return;
        }
        sb.append(",").append(root.val);
        encode(root.left, sb);
        encode(root.right, sb);
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        // base case
        if(data.length() == 0) return null;
        Queue q = new LinkedList(Arrays.asList(data.split(",")));
        return decode(q);
    }
    
    private TreeNode decode(Queue q) {
        if(q.isEmpty()) return null;
        String cur = q.poll();
        if(cur.equals("#")) return null;
        TreeNode root = new TreeNode(Integer.valueOf(cur));
        root.left = decode(q);
        root.right = decode(q);
        return root;
    }
449. 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.

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.

思路

這道題和之前不同,一般的樹變成了BST,而且要求是as compact as possible。還是可以用preorder,還是需要分隔符,但是null就不需要保存了。deserialize部分要變得復(fù)雜,left的值總是小于root的值,right的值總是大于root的值,根據(jù)這個每次recursion的時候把左邊的值都放到另一個queue里面,剩下的就是右邊的值。

復(fù)雜度

Time: O(N^2), Space: O(N)

代碼
    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        // base case
        if(root == null) return "";
        StringBuilder encoded = new StringBuilder();
        encode(root, encoded);
        return encoded.substring(1).toString();
    }
    
    private void encode(TreeNode root, StringBuilder sb) {
        if(root == null) return;
        sb.append(",").append(root.val);
        encode(root.left, sb);
        encode(root.right, sb);
    }
    
    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        // base case
        if(data.length() == 0) return null;
        Queue q = new LinkedList();
        for(String s : data.split(",")) q.offer(Integer.valueOf(s));
        return decode(q);
    }
    
    private TreeNode decode(Queue q) {
        if(q.isEmpty()) return null;
        int cur = q.poll();
        TreeNode root = new TreeNode(cur);
        Queue left = new LinkedList(); 
        while(!q.isEmpty() && q.peek() < cur) left.offer(q.poll());
        root.left = decode(left);
        root.right = decode(q);
        return root;
    }

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/66478.html

相關(guān)文章

  • leetcode449. Serialize and Deserialize BST

    摘要:題目要求將二叉搜索樹序列化和反序列化,序列化是指將樹用字符串的形式表示,反序列化是指將字符串形式的樹還原成原來的樣子。假如二叉搜索樹的節(jié)點較多,該算法將會占用大量的額外空間。 題目要求 Serialization is the process of converting a data structure or object into a sequence of bits so that...

    Honwhy 評論0 收藏0
  • 297. Serialize and Deserialize Binary Tree

    摘要:題目解答用一個特殊的符號來表示的情況這是按先序遍歷來存到中去這里用為其包含了幾乎所有這里還是挺多知識點的,后是一個可以把數(shù)組變成則是把加到這個的中去 題目:Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stor...

    focusj 評論0 收藏0
  • [LeetCode] 297. Serialize and Deserialize Binary T

    Problem 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 li...

    MRZYD 評論0 收藏0
  • leetcode297. Serialize and Deserialize Binary Tree

    摘要:因此題目中的例子的中序遍歷結(jié)果為。但是,標(biāo)準(zhǔn)的中序遍歷并不能使我們將該結(jié)果反構(gòu)造成一棵樹,我們丟失了父節(jié)點和子節(jié)點之間的關(guān)系。這里我們也可以明顯的看出來,中序遍歷需要保存的空值遠(yuǎn)遠(yuǎn)多于廣度優(yōu)先遍歷。 題目要求 Serialization is the process of converting a data structure or object into a sequence of ...

    desdik 評論0 收藏0
  • LeetCode 297. Serialize and Deserialize Binary Tre

    摘要:題目大意將二叉樹序列化,返回序列化的,和反序列化還原。解題思路技巧在于將記錄為便于將來判斷。的思想將每一層記錄下來,反序列化時也按照層級遍歷的方法依次設(shè)置為上一個里面的元素的左孩子和右孩子。變種,可以要求輸出一個,而不是 LeetCode 297. Serialize and Deserialize Binary Tree 題目大意: 將二叉樹序列化,返回序列化的String,和反序列...

    cc17 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<