亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專(zhuān)欄INFORMATION COLUMN

297. Serialize and Deserialize Binary Tree

focusj / 2066人閱讀

摘要:題目解答用一個(gè)特殊的符號(hào)來(lái)表示的情況這是按先序遍歷來(lái)存到中去這里用為其包含了幾乎所有這里還是挺多知識(shí)點(diǎn)的,后是一個(gè)可以把數(shù)組變成則是把加到這個(gè)的中去

題目:
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.

解答:

public class Codec {
    private String spliter = ",";
    //用一個(gè)特殊的符號(hào)來(lái)表示null的情況
    private String NN = "X";

    // Encodes a tree to a single string.
    public void BuildString(StringBuilder sb, TreeNode root) {
        if (root == null) {
            sb.append("X").append(spliter);
        } else {
            //這是按先序遍歷來(lái)存root到string中去
            sb.append(root.val).append(spliter);
            BuildString(sb, root.left);
            BuildString(sb, root.right);
        }
    }
    
    public String serialize(TreeNode root) {
        StringBuilder sb = new StringBuilder();
        BuildString(sb, root);
        return sb.toString();
    }

    // Decodes your encoded data to tree.
    public TreeNode BuildTree(Deque nodes) {
        String node = nodes.remove();
        if (node.equals(NN)) {
            return null;
        } else {
            //Integer.valueOf("String");
            TreeNode root = new TreeNode(Integer.valueOf(node));
            root.left = BuildTree(nodes);
            root.right = BuildTree(nodes);
            return root;
        }
    }
    
    public TreeNode deserialize(String data) {
        //deque這里用linkedlist為interface,其包含了幾乎所有resizable array
        Deque nodes = new LinkedList<>();
        //這里還是挺多知識(shí)點(diǎn)的,String.split()后是一個(gè)String[], Arrays.asList()可以把數(shù)組變成list, nodes.addAll()則是把list加到nodes這個(gè)list的中去
        nodes.addAll(Arrays.asList(data.split(spliter)));
        return BuildTree(nodes);
    }
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/64906.html

相關(guān)文章

  • [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 評(píng)論0 收藏0
  • leetcode297. Serialize and Deserialize Binary Tree

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

    desdik 評(píng)論0 收藏0
  • LeetCode 297. Serialize and Deserialize Binary Tre

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

    cc17 評(píng)論0 收藏0
  • Serialize and Deserialize Binary Tree & BST

    摘要:思路理論上說(shuō)所有遍歷的方法都可以。但是為了使和的過(guò)程都盡量最簡(jiǎn)單,是不錯(cuò)的選擇。用作為分隔符,來(lái)表示。復(fù)雜度代碼思路這道題和之前不同,一般的樹(shù)變成了,而且要求是。還是可以用,還是需要分隔符,但是就不需要保存了。 297. Serialize and Deserialize Binary Tree Serialization is the process of converting a...

    eccozhou 評(píng)論0 收藏0
  • [LintCode/LeetCode] Binary Tree Serialization

    摘要:這里要注意的是的用法。所以記住,用可以從自動(dòng)分離出數(shù)組。跳過(guò)第一個(gè)元素并放入數(shù)組最快捷語(yǔ)句建立的用意記錄處理過(guò)的結(jié)點(diǎn)并按處理所有結(jié)點(diǎn)和自己的連接下面先通過(guò)判斷,再修改的符號(hào)的順序,十分巧妙更輕便的解法 Problem Design an algorithm and write code to serialize and deserialize a binary tree. Writin...

    keithyau 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<