摘要:因此題目中的例子的中序遍歷結(jié)果為。但是,標準的中序遍歷并不能使我們將該結(jié)果反構(gòu)造成一棵樹,我們丟失了父節(jié)點和子節(jié)點之間的關(guān)系。這里我們也可以明顯的看出來,中序遍歷需要保存的空值遠遠多于廣度優(yōu)先遍歷。
題目要求
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.
設(shè)計一個方法將一個二叉樹序列化并反序列化。序列化是指將對象轉(zhuǎn)化成一個字符串,該字符串可以在網(wǎng)絡(luò)上傳輸,并且到達目的地后可以通過反序列化恢復(fù)成原來的對象。
在Leetcode中,樹的序列化是通過廣度優(yōu)先遍歷實現(xiàn)的,就如題目中所介紹的那樣。
這里我們使用中序遍歷來解決這個問題。
思路和代碼中序遍歷是指遞歸的先遍歷當前節(jié)點,然后按同樣的方式遞歸的遍歷左子樹,再遞歸的遍歷右子樹。因此題目中的例子的中序遍歷結(jié)果為[1,2,3,4,5]。但是,標準的中序遍歷并不能使我們將該結(jié)果反構(gòu)造成一棵樹,我們丟失了父節(jié)點和子節(jié)點之間的關(guān)系。因此我們需要適當?shù)牟迦肟罩祦肀4娓腹?jié)點和子節(jié)點的關(guān)系。
因此,我們將訪問到空值也記錄下來,結(jié)果如下[1,2, , ,3,4, , ,5, , ]。
這里我們也可以明顯的看出來,中序遍歷需要保存的空值遠遠多于廣度優(yōu)先遍歷。
那么我們?nèi)绾螌⑦@個字符串反序列化呢?
其實思路還是一樣的,將其根據(jù)分隔符分割后傳入隊列,不斷從隊列頭讀取數(shù)據(jù),先構(gòu)建當前節(jié)點,然后依次構(gòu)建左子樹和右子樹,如果遇到空值,則返回遞歸。
代碼如下:
private static final String NULL = "N"; private static final String SPLITOR = ","; // Encodes a tree to a single string. public String serialize(TreeNode root) { StringBuilder result = new StringBuilder(); inOrder(root, result); return result.toString(); } private void inOrder(TreeNode root, StringBuilder result){ if(root==null){ result.append(NULL).append(SPLITOR); }else{ result.append(root.val).append(SPLITOR); inOrder(root.left, result); inOrder(root.right, result); } } // Decodes your encoded data to tree. public TreeNode deserialize(String data) { Dequenodes = new LinkedList (Arrays.asList(data.split(SPLITOR))); return buildTree(nodes); } private TreeNode buildTree(Deque nodes) { String val = nodes.remove(); if (val.equals(NULL)) return null; else { TreeNode node = new TreeNode(Integer.valueOf(val)); node.left = buildTree(nodes); node.right = buildTree(nodes); return node; } }
想要了解更多開發(fā)技術(shù),面試教程以及互聯(lián)網(wǎng)公司內(nèi)推,歡迎關(guān)注我的微信公眾號!將會不定期的發(fā)放福利哦~
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/68650.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...
摘要:題目大意將二叉樹序列化,返回序列化的,和反序列化還原。解題思路技巧在于將記錄為便于將來判斷。的思想將每一層記錄下來,反序列化時也按照層級遍歷的方法依次設(shè)置為上一個里面的元素的左孩子和右孩子。變種,可以要求輸出一個,而不是 LeetCode 297. Serialize and Deserialize Binary Tree 題目大意: 將二叉樹序列化,返回序列化的String,和反序列...
摘要:題目解答用一個特殊的符號來表示的情況這是按先序遍歷來存到中去這里用為其包含了幾乎所有這里還是挺多知識點的,后是一個可以把數(shù)組變成則是把加到這個的中去 題目:Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stor...
摘要:思路理論上說所有遍歷的方法都可以。但是為了使和的過程都盡量最簡單,是不錯的選擇。用作為分隔符,來表示。復(fù)雜度代碼思路這道題和之前不同,一般的樹變成了,而且要求是。還是可以用,還是需要分隔符,但是就不需要保存了。 297. Serialize and Deserialize Binary Tree Serialization is the process of converting a...
摘要:這里要注意的是的用法。所以記住,用可以從自動分離出數(shù)組。跳過第一個元素并放入數(shù)組最快捷語句建立的用意記錄處理過的結(jié)點并按處理所有結(jié)點和自己的連接下面先通過判斷,再修改的符號的順序,十分巧妙更輕便的解法 Problem Design an algorithm and write code to serialize and deserialize a binary tree. Writin...
閱讀 2766·2023-04-26 00:07
閱讀 2496·2021-11-15 11:37
閱讀 717·2021-10-19 11:44
閱讀 2240·2021-09-22 15:56
閱讀 1824·2021-09-10 10:50
閱讀 1570·2021-08-18 10:21
閱讀 2666·2019-08-30 15:53
閱讀 1693·2019-08-30 11:11