<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)449:序列化和反序列化二叉搜索樹

          共 3016字,需瀏覽 7分鐘

           ·

          2021-11-26 19:42

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?序列化和反序列化二叉搜索樹,我們先來看題面:
          https://leetcode-cn.com/problems/serialize-and-deserialize-bst/

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


          The encoded string should be as compact as possible.

          序列化是將數(shù)據(jù)結(jié)構(gòu)或?qū)ο筠D(zhuǎn)換為一系列位的過程,以便它可以存儲在文件或內(nèi)存緩沖區(qū)中,或通過網(wǎng)絡(luò)連接鏈路傳輸,以便稍后在同一個(gè)或另一個(gè)計(jì)算機(jī)環(huán)境中重建。
          設(shè)計(jì)一個(gè)算法來序列化和反序列化 二叉搜索樹 。對序列化/反序列化算法的工作方式?jīng)]有限制。您只需確保二叉搜索樹可以序列化為字符串,并且可以將該字符串反序列化為最初的二叉搜索樹。
          編碼的字符串應(yīng)盡可能緊湊。

          示例? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

          示例 1:
          輸入:root = [2,1,3]
          輸出:[2,1,3]

          示例 2:
          輸入:root = []
          輸出:[]


          解題

          https://zhuanlan.zhihu.com/p/269128835

          主要思路:
          要進(jìn)行序列化和反序列化,我們需要把樹用一個(gè)字符串來表示,并且能夠根據(jù)這個(gè)字符串唯一地確定二叉搜索樹的形態(tài)。由于二叉搜索樹本身存在“中序遍歷得到的序列是遞增序列”這樣的性質(zhì),我們就要解決如何解決空節(jié)點(diǎn)的表示的問題 這里我們把空節(jié)點(diǎn)用"#"來表示,遍歷一遍二叉搜索樹,把樹轉(zhuǎn)化成一個(gè)字符串表示起來,這是序列化的過程。反序列化的時(shí)候,再根據(jù)序列化得到的字符串恢復(fù)二叉搜索樹。

          這里有個(gè)技巧,序列化的時(shí)候,字符串是根節(jié)點(diǎn)的值 + 一個(gè)空格 + 左子樹序列化后的字符串 + 一個(gè)空格 + 右子樹序列化后的字符串。

          因此在反序列化的時(shí)候,可以使用stringstream自動對空格進(jìn)行分割,然后最開始得到的字串就是根節(jié)點(diǎn)的值,第二段字串是左子樹,第三段字串是右子樹。在對左、右子樹進(jìn)行恢復(fù)(反序列化)的時(shí)候,可以遞歸的調(diào)用decode()方法。

          /**
          ?* Definition for a binary tree node.
          ?* struct TreeNode {
          ?* int val;
          ?* TreeNode *left;
          ?* TreeNode *right;
          ?* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
          ?* };
          ?*/

          class?Codec?{
          public:
          ????// Encodes a tree to a single string.
          ????string?serialize(TreeNode* root)?{
          ???????if(root == NULL) { // 空節(jié)點(diǎn)用"#"表示
          ???????????return?"#";
          ???????}
          ???????return?to_string(root -> val) + ' '?+ serialize(root -> left) + ' '?+ serialize(root -> right);
          ????}

          ????// Decodes your encoded data to tree.
          ????TreeNode* deserialize(string?data)?{
          ????????stringstream?ss(data);
          ????????return?decode(ss);
          ????}

          ????TreeNode* decode(stringstream?&ss)?{
          ????????string?data;
          ????????ss >> data;
          ????????if(data == "#") {
          ????????????return?NULL;
          ????????}
          ????????TreeNode* root = new?TreeNode(stoi(data)); // stringstream讀到的第一段字符串是根節(jié)點(diǎn)的值
          ????????root -> left = decode(ss); // 遞歸調(diào)用decode()方法對左右子樹建樹,并將root與左右子樹連接起來
          ????????root -> right = decode(ss);
          ????????return?root;
          ????}
          };

          // Your Codec object will be instantiated and called as such:
          // Codec* ser = new Codec();
          // Codec* deser = new Codec();
          // string tree = ser->serialize(root);
          // TreeNode* ans = deser->deserialize(tree);
          // return ans;


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-440題匯總,希望對你有點(diǎn)幫助!

          LeetCode刷題實(shí)戰(zhàn)441:排列硬幣

          LeetCode刷題實(shí)戰(zhàn)442:數(shù)組中重復(fù)的數(shù)據(jù)

          LeetCode刷題實(shí)戰(zhàn)443:壓縮字符串

          LeetCode刷題實(shí)戰(zhàn)444:序列重建

          LeetCode刷題實(shí)戰(zhàn)445:兩數(shù)相加 II

          LeetCode刷題實(shí)戰(zhàn)446:等差數(shù)列劃分 II - 子序列

          LeetCode刷題實(shí)戰(zhàn)447:回旋鏢的數(shù)量

          LeetCode刷題實(shí)戰(zhàn)448:找到所有數(shù)組中消失的數(shù)字


          瀏覽 29
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  精品国产乱码 | 欧美成人精品一二三区欧美风情 | 成人免费在线视频网站 | 欧美大陆三级成人网站 | 老鸭窝日本天堂中文字幕在线免费观看 |