{"id":"ef8957fb-20a7-4177-8e4d-ce69c26f68c5","name":"Serialize And Deserialize Binary Tree","description":"1. Serialization is to store tree in a file so that it can be later restored.\r\n2. The structure of tree must be maintained. Deserialization is reading tree back from file.\r\n","inputFormat":"Input is managed for you.\r\n","outputFormat":"Output is managed for you.\r\n","constraints":"0 &lt;= Number of Nodes &lt;= 10^5\r\n-1000 &lt;= value of Node data &lt;= 1000\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\r\n#include <queue>\r\n#include <stringstream>\r\nusing namespace std;\r\n\r\nclass TreeNode\r\n{\r\npublic:\r\n int val = 0;\r\n TreeNode *left = nullptr;\r\n TreeNode *right = nullptr;\r\n\r\n TreeNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\n// Encodes a tree to a single string.\r\nstring serialize(TreeNode *root)\r\n{\r\n}\r\n\r\n// Decodes your encoded data to tree.\r\nTreeNode *deserialize(string s)\r\n{\r\n}\r\n\r\n// input_Section_====================================================================\r\n\r\nvoid display(TreeNode *node)\r\n{\r\n if (node == nullptr)\r\n return;\r\n\r\n string str = \"\";\r\n str += ((node->left != nullptr ? to_string(node->left->val) : \".\"));\r\n str += (\" -> \" + to_string(node->val) + \" <- \");\r\n str += ((node->right != nullptr ? to_string(node->right->val) : \".\"));\r\n\r\n cout << str << endl;\r\n\r\n display(node->left);\r\n display(node->right);\r\n}\r\n\r\nTreeNode *createTree(vector<int> &arr, vector<int> &IDX)\r\n{\r\n\r\n if (IDX[0] > arr.size() || arr[IDX[0]] == -1)\r\n {\r\n IDX[0]++;\r\n return nullptr;\r\n }\r\n\r\n TreeNode *node = new TreeNode(arr[IDX[0]++]);\r\n node->left = createTree(arr, IDX);\r\n node->right = createTree(arr, IDX);\r\n\r\n return node;\r\n}\r\n\r\nvoid solve()\r\n{\r\n int n;\r\n cin >> n;\r\n vector<int> arr(n, 0);\r\n for (int i = 0; i < n; i++)\r\n {\r\n cin >> arr[i];\r\n }\r\n\r\n vector<int> IDX(1, 0);\r\n TreeNode *root = createTree(arr, IDX);\r\n\r\n string s = serialize(root);\r\n display(deserialize(s));\r\n}\r\n\r\nint main()\r\n{\r\n solve();\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class TreeNode {\r\n int val = 0;\r\n TreeNode left = null;\r\n TreeNode right = null;\r\n\r\n TreeNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n // Encodes a tree to a single string.\r\n public static String serialize(TreeNode root) {\r\n\r\n }\r\n\r\n // Decodes your encoded data to tree.\r\n public static TreeNode deserialize(String str) {\r\n\r\n }\r\n\r\n // input_section=================================================\r\n\r\n public static void display(TreeNode node) {\r\n if (node == null)\r\n return;\r\n\r\n StringBuilder sb = new StringBuilder();\r\n sb.append((node.left != null ? node.left.val : \".\"));\r\n sb.append(\" -> \" + node.val + \" <- \");\r\n sb.append((node.right != null ? node.right.val : \".\"));\r\n\r\n System.out.println(sb.toString());\r\n\r\n display(node.left);\r\n display(node.right);\r\n\r\n }\r\n\r\n public static TreeNode createTree(int[] arr, int[] IDX) {\r\n if (IDX[0] > arr.length || arr[IDX[0]] == -1) {\r\n IDX[0]++;\r\n return null;\r\n }\r\n TreeNode node = new TreeNode(arr[IDX[0]++]);\r\n node.left = createTree(arr, IDX);\r\n node.right = createTree(arr, IDX);\r\n\r\n return node;\r\n }\r\n\r\n public static void solve() {\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for (int i = 0; i < n; i++)\r\n arr[i] = scn.nextInt();\r\n\r\n int[] IDX = new int[1];\r\n TreeNode root = createTree(arr, IDX);\r\n\r\n String s = serialize(root);\r\n display(deserialize(s));\r\n }\r\n\r\n public static void main(String[] args) {\r\n solve();\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"15\r\n1\r\n1\r\n-1\r\n1\r\n1\r\n-1\r\n1\r\n-1\r\n-1\r\n1\r\n-1\r\n-1\r\n1\r\n-1\r\n-1","sampleOutput":"1 -> 1 <- 1\r\n. -> 1 <- 1\r\n1 -> 1 <- 1\r\n. -> 1 <- 1\r\n. -> 1 <- .\r\n. -> 1 <- .\r\n. -> 1 <- .\r\n","questionVideo":"https://www.youtube.com/embed/qzdTHpuP434","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"2e9df04c-be14-441c-9a18-8b5a8ca6596d","name":"Trees For Intermediate","slug":"trees-for-intermediate-9994","type":0},{"id":"8507b1db-574f-43b9-ae0e-ba5357debc80","name":"Serialize And Deserialize Binary Tree","slug":"serialize-and-deserialize-binary-tree","type":1}],"next":{"id":"9edd58c9-3097-4c91-b4d9-4c2ef255ff4a","name":"Serialize and deserialize binary tree","type":3,"slug":"serialize-and-deserialize-binary-tree"},"prev":{"id":"b1a83b71-9ef5-485c-a7df-851b3dfc9aa7","name":"Longest Zigzag Path In A Binary Tree","type":0,"slug":"longest-zigzag-path-in-a-binary-tree"}}}

Serialize And Deserialize Binary Tree

1. Serialization is to store tree in a file so that it can be later restored. 2. The structure of tree must be maintained. Deserialization is reading tree back from file.

{"id":"ef8957fb-20a7-4177-8e4d-ce69c26f68c5","name":"Serialize And Deserialize Binary Tree","description":"1. Serialization is to store tree in a file so that it can be later restored.\r\n2. The structure of tree must be maintained. Deserialization is reading tree back from file.\r\n","inputFormat":"Input is managed for you.\r\n","outputFormat":"Output is managed for you.\r\n","constraints":"0 &lt;= Number of Nodes &lt;= 10^5\r\n-1000 &lt;= value of Node data &lt;= 1000\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\r\n#include <queue>\r\n#include <stringstream>\r\nusing namespace std;\r\n\r\nclass TreeNode\r\n{\r\npublic:\r\n int val = 0;\r\n TreeNode *left = nullptr;\r\n TreeNode *right = nullptr;\r\n\r\n TreeNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\n// Encodes a tree to a single string.\r\nstring serialize(TreeNode *root)\r\n{\r\n}\r\n\r\n// Decodes your encoded data to tree.\r\nTreeNode *deserialize(string s)\r\n{\r\n}\r\n\r\n// input_Section_====================================================================\r\n\r\nvoid display(TreeNode *node)\r\n{\r\n if (node == nullptr)\r\n return;\r\n\r\n string str = \"\";\r\n str += ((node->left != nullptr ? to_string(node->left->val) : \".\"));\r\n str += (\" -> \" + to_string(node->val) + \" <- \");\r\n str += ((node->right != nullptr ? to_string(node->right->val) : \".\"));\r\n\r\n cout << str << endl;\r\n\r\n display(node->left);\r\n display(node->right);\r\n}\r\n\r\nTreeNode *createTree(vector<int> &arr, vector<int> &IDX)\r\n{\r\n\r\n if (IDX[0] > arr.size() || arr[IDX[0]] == -1)\r\n {\r\n IDX[0]++;\r\n return nullptr;\r\n }\r\n\r\n TreeNode *node = new TreeNode(arr[IDX[0]++]);\r\n node->left = createTree(arr, IDX);\r\n node->right = createTree(arr, IDX);\r\n\r\n return node;\r\n}\r\n\r\nvoid solve()\r\n{\r\n int n;\r\n cin >> n;\r\n vector<int> arr(n, 0);\r\n for (int i = 0; i < n; i++)\r\n {\r\n cin >> arr[i];\r\n }\r\n\r\n vector<int> IDX(1, 0);\r\n TreeNode *root = createTree(arr, IDX);\r\n\r\n string s = serialize(root);\r\n display(deserialize(s));\r\n}\r\n\r\nint main()\r\n{\r\n solve();\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class TreeNode {\r\n int val = 0;\r\n TreeNode left = null;\r\n TreeNode right = null;\r\n\r\n TreeNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n // Encodes a tree to a single string.\r\n public static String serialize(TreeNode root) {\r\n\r\n }\r\n\r\n // Decodes your encoded data to tree.\r\n public static TreeNode deserialize(String str) {\r\n\r\n }\r\n\r\n // input_section=================================================\r\n\r\n public static void display(TreeNode node) {\r\n if (node == null)\r\n return;\r\n\r\n StringBuilder sb = new StringBuilder();\r\n sb.append((node.left != null ? node.left.val : \".\"));\r\n sb.append(\" -> \" + node.val + \" <- \");\r\n sb.append((node.right != null ? node.right.val : \".\"));\r\n\r\n System.out.println(sb.toString());\r\n\r\n display(node.left);\r\n display(node.right);\r\n\r\n }\r\n\r\n public static TreeNode createTree(int[] arr, int[] IDX) {\r\n if (IDX[0] > arr.length || arr[IDX[0]] == -1) {\r\n IDX[0]++;\r\n return null;\r\n }\r\n TreeNode node = new TreeNode(arr[IDX[0]++]);\r\n node.left = createTree(arr, IDX);\r\n node.right = createTree(arr, IDX);\r\n\r\n return node;\r\n }\r\n\r\n public static void solve() {\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for (int i = 0; i < n; i++)\r\n arr[i] = scn.nextInt();\r\n\r\n int[] IDX = new int[1];\r\n TreeNode root = createTree(arr, IDX);\r\n\r\n String s = serialize(root);\r\n display(deserialize(s));\r\n }\r\n\r\n public static void main(String[] args) {\r\n solve();\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"15\r\n1\r\n1\r\n-1\r\n1\r\n1\r\n-1\r\n1\r\n-1\r\n-1\r\n1\r\n-1\r\n-1\r\n1\r\n-1\r\n-1","sampleOutput":"1 -> 1 <- 1\r\n. -> 1 <- 1\r\n1 -> 1 <- 1\r\n. -> 1 <- 1\r\n. -> 1 <- .\r\n. -> 1 <- .\r\n. -> 1 <- .\r\n","questionVideo":"https://www.youtube.com/embed/qzdTHpuP434","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"2e9df04c-be14-441c-9a18-8b5a8ca6596d","name":"Trees For Intermediate","slug":"trees-for-intermediate-9994","type":0},{"id":"8507b1db-574f-43b9-ae0e-ba5357debc80","name":"Serialize And Deserialize Binary Tree","slug":"serialize-and-deserialize-binary-tree","type":1}],"next":{"id":"9edd58c9-3097-4c91-b4d9-4c2ef255ff4a","name":"Serialize and deserialize binary tree","type":3,"slug":"serialize-and-deserialize-binary-tree"},"prev":{"id":"b1a83b71-9ef5-485c-a7df-851b3dfc9aa7","name":"Longest Zigzag Path In A Binary Tree","type":0,"slug":"longest-zigzag-path-in-a-binary-tree"}}}
plane

Editor


Loading...

Serialize And Deserialize Binary Tree

easy

1. Serialization is to store tree in a file so that it can be later restored. 2. The structure of tree must be maintained. Deserialization is reading tree back from file.

Constraints

0 <= Number of Nodes <= 10^5 -1000 <= value of Node data <= 1000

Format

Input

Input is managed for you.

Output

Output is managed for you.

Example

Sample Input

15 1 1 -1 1 1 -1 1 -1 -1 1 -1 -1 1 -1 -1

Sample Output

1 -> 1

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode