File tree 3 files changed +172
-0
lines changed
solution/0200-0299/0297.Serialize and Deserialize Binary Tree
3 files changed +172
-0
lines changed Original file line number Diff line number Diff line change 74
74
75
75
```
76
76
77
+ ### ** JavaScript**
78
+
79
+ ``` js
80
+ /**
81
+ * Definition for a binary tree node.
82
+ * function TreeNode(val) {
83
+ * this.val = val;
84
+ * this.left = this.right = null;
85
+ * }
86
+ */
87
+
88
+ /**
89
+ * Encodes a tree to a single string.
90
+ *
91
+ * @param {TreeNode} root
92
+ * @return {string}
93
+ */
94
+ var serialize = function (root ) {
95
+ let data = [];
96
+ let serializeRec = function (root ) {
97
+ if (! root) {
98
+ data .push (1001 );
99
+ return null ;
100
+ }
101
+ data .push (root .val );
102
+
103
+ serializeRec (root .left );
104
+ serializeRec (root .right );
105
+ }
106
+ serializeRec (root);
107
+ return data;
108
+ };
109
+
110
+ /**
111
+ * Decodes your encoded data to tree.
112
+ *
113
+ * @param {string} data
114
+ * @return {TreeNode}
115
+ */
116
+ var deserialize = function (data ) {
117
+ if (! data) {
118
+ return null ;
119
+ }
120
+ let curVal = data .shift ();
121
+ if (curVal == 1001 ) {
122
+ return null ;
123
+ }
124
+ let node = new TreeNode (curVal);
125
+ node .left = deserialize (data);
126
+ node .right = deserialize (data);
127
+ return node;
128
+ };
129
+
130
+ /**
131
+ * Your functions will be called as such:
132
+ * deserialize(serialize(root));
133
+ */
134
+ ```
135
+
77
136
### ** ...**
78
137
79
138
```
Original file line number Diff line number Diff line change 64
64
65
65
```
66
66
67
+ ### ** JavaScript**
68
+
69
+ ``` js
70
+ /**
71
+ * Definition for a binary tree node.
72
+ * function TreeNode(val) {
73
+ * this.val = val;
74
+ * this.left = this.right = null;
75
+ * }
76
+ */
77
+
78
+ /**
79
+ * Encodes a tree to a single string.
80
+ *
81
+ * @param {TreeNode} root
82
+ * @return {string}
83
+ */
84
+ var serialize = function (root ) {
85
+ let data = [];
86
+ let serializeRec = function (root ) {
87
+ if (! root) {
88
+ data .push (1001 );
89
+ return null ;
90
+ }
91
+ data .push (root .val );
92
+
93
+ serializeRec (root .left );
94
+ serializeRec (root .right );
95
+ }
96
+ serializeRec (root);
97
+ return data;
98
+ };
99
+
100
+ /**
101
+ * Decodes your encoded data to tree.
102
+ *
103
+ * @param {string} data
104
+ * @return {TreeNode}
105
+ */
106
+ var deserialize = function (data ) {
107
+ if (! data) {
108
+ return null ;
109
+ }
110
+ let curVal = data .shift ();
111
+ if (curVal == 1001 ) {
112
+ return null ;
113
+ }
114
+ let node = new TreeNode (curVal);
115
+ node .left = deserialize (data);
116
+ node .right = deserialize (data);
117
+ return node;
118
+ };
119
+
120
+ /**
121
+ * Your functions will be called as such:
122
+ * deserialize(serialize(root));
123
+ */
124
+ ```
125
+
67
126
### ** ...**
68
127
69
128
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * function TreeNode(val) {
4
+ * this.val = val;
5
+ * this.left = this.right = null;
6
+ * }
7
+ */
8
+
9
+ /**
10
+ * Encodes a tree to a single string.
11
+ *
12
+ * @param {TreeNode } root
13
+ * @return {string }
14
+ */
15
+ var serialize = function ( root ) {
16
+ let data = [ ] ;
17
+ let serializeRec = function ( root ) {
18
+ if ( ! root ) {
19
+ data . push ( 1001 ) ;
20
+ return null ;
21
+ }
22
+ data . push ( root . val ) ;
23
+
24
+ serializeRec ( root . left ) ;
25
+ serializeRec ( root . right ) ;
26
+ }
27
+ serializeRec ( root ) ;
28
+ return data ;
29
+ } ;
30
+
31
+ /**
32
+ * Decodes your encoded data to tree.
33
+ *
34
+ * @param {string } data
35
+ * @return {TreeNode }
36
+ */
37
+ var deserialize = function ( data ) {
38
+ if ( ! data ) {
39
+ return null ;
40
+ }
41
+ let curVal = data . shift ( ) ;
42
+ if ( curVal == 1001 ) {
43
+ return null ;
44
+ }
45
+ let node = new TreeNode ( curVal ) ;
46
+ node . left = deserialize ( data ) ;
47
+ node . right = deserialize ( data ) ;
48
+ return node ;
49
+ } ;
50
+
51
+ /**
52
+ * Your functions will be called as such:
53
+ * deserialize(serialize(root));
54
+ */
You can’t perform that action at this time.
0 commit comments