-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathSerialize and Deserialize BST.js
81 lines (64 loc) · 2.11 KB
/
Serialize and Deserialize BST.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Runtime: 69 ms (Top 97.8%) | Memory: 50.68 MB (Top 26.8%)
;/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* Encodes a tree to a single string.
*
* @param {TreeNode} root
* @return {string}
*/
var serialize = function(root) {
// Using Preorder traversal to create a string of BST
// Preorder works in following way
// root -> left -> right
let preorder = [];
function dfs(node) {
if (node === null) return;
// Get root value
preorder.push(node.val);
// Get All the Left values
dfs(node.left);
// Get all the right values
dfs(node.right);
}
// call it with root
dfs(root)
// Turn into string and return it
return preorder.join(',')
};
/**
* Decodes your encoded data to tree.
*
* @param {string} data
* @return {TreeNode}
*/
var deserialize = function(data) {
if (data === "") return null;
// Get numbers array from a string
const preorder = data.split(',').map(Number);
// using -Infinity and +Infinity as placeholder check
function recur(lower = -Infinity, upper = Infinity) {
// This condition useful for when we are filling left side of tree it'll avoid all the values greater then then its upper value by putting null init.
if (preorder[0] < lower || preorder[0] > upper) return null;
// If preorder become empty
if (preorder.length === 0) return null;
// Create a root node [shift method will change the original array]
const root = new TreeNode(preorder.shift());
// Here for left side of tree, we are using current root node's value as 'upper bound' (so higher values ignored).
root.left = recur(lower, root.val);
// Same as above for right side we are using root node's values as 'lower bound' (so lower values ignored);
root.right = recur(root.val, upper);
return root;
}
// Final root will be out BST
return recur();
};
/**
* Your functions will be called as such:
* deserialize(serialize(root));
*/