<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刷題實戰(zhàn)297:二叉樹的序列化與反序列化

          共 6028字,需瀏覽 13分鐘

           ·

          2021-06-17 20:47

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

          今天和大家聊的問題叫做 二叉樹的序列化與反序列化,我們先來看題面:
          https://leetcode-cn.com/problems/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 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.

          Clarification: The input/output format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.

          序列化是將一個數(shù)據(jù)結構或者對象轉換為連續(xù)的比特位的操作,進而可以將轉換后的數(shù)據(jù)存儲在一個文件或者內存中,同時也可以通過網(wǎng)絡傳輸?shù)搅硪粋€計算機環(huán)境,采取相反方式重構得到原數(shù)據(jù)。

          請設計一個算法來實現(xiàn)二叉樹的序列化與反序列化。這里不限定你的序列 / 反序列化算法執(zhí)行邏輯,你只需要保證一個二叉樹可以被序列化為一個字符串并且將這個字符串反序列化為原始的樹結構。

          提示: 輸入輸出格式與 LeetCode 目前使用的方式一致,詳情請參閱 LeetCode 序列化二叉樹的格式。你并非必須采取這種方式,你也可以采用其他的方法解決這個問題。

          示例


          解題


          序列化
          用一個string保存序列。
          使用先序遍歷,遇到空結點,字符串保存‘#’。否則,當前節(jié)點的數(shù)字保存為字符串,用’!'號結尾。比如上面的例子,序列化后為“1!2!3!##4!5!”。

          反序列化
          遍歷字符串,如果遇到‘#’,返回nullptr。否則根據(jù)’!'號,找到節(jié)點的數(shù)字字符串,轉化為數(shù)字,創(chuàng)建新的節(jié)點。然后左右節(jié)點遞歸調用。

          /**
           * 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 == nullptr ) return "";

                  string ans = "";
                  serializeCore( root, ans );

                  return ans;
              }

              void serializeCore( TreeNode* node, string& seq ) {
                  if ( node == nullptr ) {
                      seq += '#';
                      return;
                  }

                  seq += to_string( node->val ) + '!';

                  serializeCore( node->left, seq );
                  serializeCore( node->right, seq );

                  return;
              }

              // Decodes your encoded data to tree.
              TreeNode* deserialize(string data) {
                  if ( data == "" )
                      return nullptr;
                      
                  int index = 0;

                  return deserializeCore( data, index );
              }

              TreeNode* deserializeCore( string& str, int& index ) {
                  if ( str[index] == '#' ) {
                      ++index;
                      return nullptr;
                  }
                  
                  int left = index;
                  while ( str[index] != '!' )
                      ++index;
                  
                  string temp = str.substr( left, index - left );
                  TreeNode* node = new TreeNode( stoi( temp ) );
                  ++index;

                  node->left = deserializeCore( str, index );
                  node->right = deserializeCore( str, index );

                  return node;
              }

              int stoi( string& str ) {
                  int res = 0;
                  int sign = 1;
                  int i = 0;

                  if ( str[0] == '-' ) {
                      sign = -1;
                      i = 1;
                  }

                  while ( i < str.size() )
                      res = res * 10 + ( str[i++] - '0' );

                  return sign * res;
              }
          };


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

          上期推文:
          LeetCode1-280題匯總,希望對你有點幫助!
          LeetCode刷題實戰(zhàn)281:鋸齒迭代器
          LeetCode刷題實戰(zhàn)282:給表達式添加運算符
          LeetCode刷題實戰(zhàn)283:移動零
          LeetCode刷題實戰(zhàn)284:頂端迭代器
          LeetCode刷題實戰(zhàn)285:二叉搜索樹中的順序后繼
          LeetCode刷題實戰(zhàn)286:墻和門
          LeetCode刷題實戰(zhàn)287:尋找重復數(shù)
          LeetCode刷題實戰(zhàn)288:單詞的唯一縮寫
          LeetCode刷題實戰(zhàn)289:生命游戲
          LeetCode刷題實戰(zhàn)290:單詞規(guī)律
          LeetCode刷題實戰(zhàn)291:單詞規(guī)律II
          LeetCode刷題實戰(zhàn)292:Nim 游戲
          LeetCode刷題實戰(zhàn)293:翻轉游戲
          LeetCode刷題實戰(zhàn)294:翻轉游戲II
          LeetCode刷題實戰(zhàn)295:數(shù)據(jù)流的中位數(shù)
          LeetCode刷題實戰(zhàn)296:最佳的碰頭地點

          瀏覽 33
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <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>
                  欧美操操 | 啪啪啪网站观看 | 十八禁成人黄网站 | 在线欧美日本 | 亚洲中文字幕一二三无码欧美 |