Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- // Definition for a Node.
- class Node {
- public:
- int val;
- vector<Node*> children;
- Node() {}
- Node(int _val) {
- val = _val;
- }
- Node(int _val, vector<Node*> _children) {
- val = _val;
- children = _children;
- }
- };
- */
- class Codec {
- public:
- string serialize(Node* root) {
- if(!root)return "#";
- string str = to_string(root->val);
- for(Node* n : root->children)
- str += "," + serialize(n);
- str += ",#";
- return str;
- }
- // Decodes your encoded data to tree.
- Node* deserialize(stringstream& ss) {
- string node;
- getline(ss, node, ',');
- if(node == "#"){
- return nullptr;
- }
- Node* root = new Node( stoi(node) );
- Node* child = deserialize(ss);
- while(child){
- root->children.push_back(child);
- child = deserialize(ss);
- }
- return root;
- }
- Node* deserialize(string data) {
- stringstream ss(data);
- return deserialize(ss);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment