problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::serialize_and_deserialize_binary_tree::Codec类 参考

#include <leetcode.h>

静态 Public 成员函数

static TreeNodedeserialize (string data)
 Decodes your encoded data to tree. 更多...
 
static string serialize (TreeNode *root)
 Encodes a tree to a single string. 更多...
 

详细描述

在文件 leetcode.h2605 行定义.

成员函数说明

◆ deserialize()

TreeNode * leetcode::serialize_and_deserialize_binary_tree::Codec::deserialize ( string  data)
static

Decodes your encoded data to tree.

在文件 leetcode.cpp7045 行定义.

7045 {
7046 if(data == "[]") {
7047 return nullptr;
7048 }
7049 vector<string> vec;
7050 data = data.substr(1, data.size() - 2);
7051 auto *const str = static_cast<char *>(malloc((data.length() + 1) * sizeof(char)));
7052 memcpy(str, data.c_str(), (data.length() + 1) * sizeof(char));
7053 for(const char *item = strtok(str, ","); item != nullptr; item = strtok(nullptr, ",")) {
7054 vec.emplace_back(string(item));
7055 }
7056 queue<TreeNode **> q;
7057 auto *root = new TreeNode(stoi(vec[0]));
7058 q.push(&root);
7059 for(const auto &str: vec) {
7060 auto *const node = q.front();
7061 q.pop();
7062 if(str == "null") {
7063 *node = nullptr;
7064 } else {
7065 *node = new TreeNode(stoi(str));
7066 q.push(&(*node)->left);
7067 q.push(&(*node)->right);
7068 }
7069 }
7070 return root;
7071 }
vector< int > root
Definition: acwing408.cpp:349
int vec[100010]
Definition: pat.cpp:5095

引用了 acwing::acwing836_408::root , 以及 pat::a::a7_2::vec.

被这些函数引用 leetcode::delete_node_in_a_bst::TEST() , 以及 leetcode::serialize_and_deserialize_binary_tree::TEST().

◆ serialize()

string leetcode::serialize_and_deserialize_binary_tree::Codec::serialize ( TreeNode root)
static

Encodes a tree to a single string.

在文件 leetcode.cpp7013 行定义.

7013 {
7014 if(root == nullptr) {
7015 return "[]";
7016 }
7017 vector<string> vec;
7018 ostringstream oss;
7019 oss << '[';
7020 queue<TreeNode *> q;
7021 q.push(root);
7022 while(!q.empty()) {
7023 const TreeNode *node = q.front();
7024 q.pop();
7025 if(node == nullptr) {
7026 vec.emplace_back("null");
7027 continue;
7028 }
7029 vec.emplace_back(to_string(node->val));
7030 q.push(node->left);
7031 q.push(node->right);
7032 }
7033 int end = vec.size() - 1;
7034 while(vec[end] == "null" && end > 0) {
7035 end--;
7036 }
7037 oss << vec[0];
7038 for(int i = 1; i <= end; i++) {
7039 oss << ',' << vec[i];
7040 }
7041 oss << ']';
7042 return oss.str();
7043 }

引用了 leetcode::TreeNode::left, leetcode::TreeNode::right, acwing::acwing836_408::root, leetcode::TreeNode::val , 以及 pat::a::a7_2::vec.


该类的文档由以下文件生成: