【LeetCode】297. Serialize and Deserialize Binary Tree

二叉树的序列化与反序列化。

如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便。

这里学会了使用 sstream 中的 输入流‘istringstream‘ 和 输出流‘ostringstream‘.

istringstream in;

in >> str;

这里没执行一次就会倒出一个string (因为in流中使用了‘ ‘空格 作为分割符, 所以可以分成很多个string)

建树的时候使用先序建立二叉树。

关键代码如下:

TreeNode* build(istringstream in){
  if(#) return null;
  else{
     new a node
     newnode -> left  = build(in)
     newnode -> right = build(in)
  }
}
/**
 * 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) {
        ostringstream out;
        backtrack(root, out);
        return out.str();
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        istringstream in(data);
        return build(in);
    }

private:
    void backtrack(TreeNode *rt, ostringstream &out){
        if(rt){
            out << rt -> val << " ";
            backtrack(rt -> left, out);
            backtrack(rt -> right, out);
        }else{
            out << "#"<<" ";
        }
    }

    // 形如: 1 # 2 其中树节点必为满树空节点用#表示。中间用空格分割。
    TreeNode* build(istringstream &in){
        string str = "";
        in >> str;
        if(str == "#" || str == ""){
            return NULL;
        }else{
            TreeNode *rt = new TreeNode(atoi(str.c_str()));
            rt -> left = build(in);
            rt -> right = build(in);
            return rt;
        }
    }
};

// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));
时间: 2024-10-25 14:19:04

【LeetCode】297. Serialize and Deserialize Binary Tree的相关文章

LeetCode OJ 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 stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another comput

[Lintcode]7. Serialize and Deserialize Binary Tree/[Leetcode]297. Serialize and Deserialize Binary Tree

7. Serialize and Deserialize Binary Tree/297. Serialize and Deserialize Binary Tree 本题难度: Medium/Hard Topic: Binary Tree Description Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called '

297. Serialize and Deserialize Binary Tree

297. Serialize and Deserialize Binary Tree Total Accepted: 47713 Total Submissions: 149430 Difficulty: Hard Contributors: Admin Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in

[leetcode]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 stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another comput

LeetCode 297: Serialize and Deserialize Binary Tree

1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Codec { 11 12 // Encodes a tree to a single string. 13 public Str

难1 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 stored in a file or memory buffer,or transmitted across a network connection link to be reconstructed later in the same or another compute

【LeetCode】104 - Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Solution: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val;

[LeetCode][JavaScript]Serialize and Deserialize Binary Tree

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 reconstruct

【一天一道LeetCode】#104. Maximum Depth of Binary Tree

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:https://leetcode.com/problems/maximum-depth-of-binary-tree/ Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along th