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

資訊專欄INFORMATION COLUMN

LeetCode 297. Serialize and Deserialize Binary Tre

cc17 / 3135人閱讀

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

LeetCode 297. Serialize and Deserialize Binary Tree

題目大意: 將二叉樹序列化,返回序列化的String,和反序列化還原。

解題思路:
技巧在于將null記錄為#便于將來判斷。有兩種解法。

Level Order Traversal - BFS的思想將每一層記錄下來,反序列化時也按照層級遍歷的方法依次設置為上一個queue里面的元素的左孩子和右孩子。

更好的方法為preorder traversal,是可以handle變種題目的解法,利用preorder是root—>left->right的順序,用一個deque來不斷把頭部的元素poll出,遞歸調用函數構建還原二叉樹。

    //Solution 1: using Level order traversal
    public static String serialize(TreeNode root) {
        if (root == null ) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        Queue q = new LinkedList();
        q.offer(root);
        while(!q.isEmpty()) {
            TreeNode curr = q.poll();
            if (curr == null) {
                sb.append("#,");
            } else {
                sb.append(curr.val + ",");
                q.offer(curr.left);
                q.offer(curr.right);
            }
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    /**
     *      1
     *     / 
     *    2   3
     *   /   / 
     *  4   2   4
     *  /
     * 4
     * [1,2,3,4,#,2,4,4]
     * @param input
     * @return
     */

    public static TreeNode deSerialize(String input) {
        if (input == null || input.length() == 0) {
            return null;
        }
        String[] strs = input.split(",");
        TreeNode root = new TreeNode(Integer.valueOf(strs[0]));
        Queue q = new LinkedList();
        q.offer(root);

        for(int i = 1; i < strs.length; i++){
            TreeNode curr = q.poll();
            if (curr == null) continue;
            if (!strs[i].equals("#")) {
                curr.left = new TreeNode(Integer.valueOf(strs[i]));
                q.offer(curr.left);
            }

            i++;

            if (i < strs.length && !strs[i].equals("#")) {
                curr.right = new TreeNode(Integer.valueOf(strs[i]));
                q.offer(curr.right);
            }

        }
        return root;

    }
    //Solution II: using 2 preorder traversal
    private static final String DELIMITER = ",";
    private static final String NN = "#";

    // Encodes a tree to a single string.
    public static String serialize2(TreeNode root) {
        //using preorder traversal
        StringBuilder res = new StringBuilder();
        serializeHelper(root, res);
        res.deleteCharAt(res.length() - 1);
        return res.toString();
    }
    private static void serializeHelper(TreeNode root, StringBuilder res) {
        if (root == null) {
            res.append(NN).append(DELIMITER);
            return;
        }
        res.append(root.val).append(DELIMITER);
        serializeHelper(root.left, res);
        serializeHelper(root.right, res);
    }

    // Decodes your encoded data to tree.
    public static TreeNode deserialize2(String data) {
        Deque deque = new LinkedList<>();
        deque.addAll(Arrays.asList(data.split(DELIMITER)));
        return deserializeHelper(deque);
    }
    private static TreeNode deserializeHelper(Deque deque) {
        String now = deque.pollFirst();
        if (now.equals(NN)) {
            return null;
        }
        TreeNode node = new TreeNode(Integer.parseInt(now));
        node.left = deserializeHelper(deque);
        node.right = deserializeHelper(deque);
        return node;
    }
    public static void main(String[] args) {
        //solution 1 level order
        TreeNode root1 = deSerialize("1,2,3,4,#,5,6,7");
        String result1 = serialize(root1);
        System.out.println(result1);

        //solution 2 preorder - 變種,可以要求輸出一個LinkedList,而不是String
        TreeNode root2 = deserialize2("3,4,1,#,#,2,#,#,5,#,6,#,#");
        String result2 = serialize2(root2);

        System.out.println(result2);

    }

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/71807.html

相關文章

  • [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

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

    desdik 評論0 收藏0
  • 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 stor...

    focusj 評論0 收藏0
  • Serialize and Deserialize Binary Tree & BST

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

    eccozhou 評論0 收藏0
  • leetcode449. Serialize and Deserialize BST

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

    Honwhy 評論0 收藏0

發表評論

0條評論

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