摘要:題目大意將二叉樹(shù)序列化,返回序列化的,和反序列化還原。解題思路技巧在于將記錄為便于將來(lái)判斷。的思想將每一層記錄下來(lái),反序列化時(shí)也按照層級(jí)遍歷的方法依次設(shè)置為上一個(gè)里面的元素的左孩子和右孩子。變種,可以要求輸出一個(gè),而不是
LeetCode 297. Serialize and Deserialize Binary Tree
題目大意: 將二叉樹(shù)序列化,返回序列化的String,和反序列化還原。
解題思路:
技巧在于將null記錄為#便于將來(lái)判斷。有兩種解法。
Level Order Traversal - BFS的思想將每一層記錄下來(lái),反序列化時(shí)也按照層級(jí)遍歷的方法依次設(shè)置為上一個(gè)queue里面的元素的左孩子和右孩子。
更好的方法為preorder traversal,是可以handle變種題目的解法,利用preorder是root—>left->right的順序,用一個(gè)deque來(lái)不斷把頭部的元素poll出,遞歸調(diào)用函數(shù)構(gòu)建還原二叉樹(shù)。
//Solution 1: using Level order traversal public static String serialize(TreeNode root) { if (root == null ) { return ""; } StringBuilder sb = new StringBuilder(); Queueq = 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 - 變種,可以要求輸出一個(gè)LinkedList,而不是String TreeNode root2 = deserialize2("3,4,1,#,#,2,#,#,5,#,6,#,#"); String result2 = serialize2(root2); System.out.println(result2); }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/71807.html
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...
摘要:因此題目中的例子的中序遍歷結(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 ...
摘要:題目解答用一個(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 stor...
摘要:思路理論上說(shuō)所有遍歷的方法都可以。但是為了使和的過(guò)程都盡量最簡(jiǎn)單,是不錯(cuò)的選擇。用作為分隔符,來(lái)表示。復(fù)雜度代碼思路這道題和之前不同,一般的樹(shù)變成了,而且要求是。還是可以用,還是需要分隔符,但是就不需要保存了。 297. Serialize and Deserialize Binary Tree Serialization is the process of converting a...
摘要:題目要求將二叉搜索樹(shù)序列化和反序列化,序列化是指將樹(shù)用字符串的形式表示,反序列化是指將字符串形式的樹(shù)還原成原來(lái)的樣子。假如二叉搜索樹(shù)的節(jié)點(diǎn)較多,該算法將會(huì)占用大量的額外空間。 題目要求 Serialization is the process of converting a data structure or object into a sequence of bits so that...
閱讀 993·2021-11-24 09:39
閱讀 2214·2021-11-16 11:54
閱讀 2096·2021-11-11 17:22
閱讀 2382·2021-09-30 09:55
閱讀 3611·2021-08-12 13:22
閱讀 1638·2019-08-30 15:44
閱讀 1180·2019-08-29 12:12
閱讀 3275·2019-08-27 10:58