@@ -76,32 +76,363 @@ tags:
76
76
77
77
<!-- solution:start -->
78
78
79
- ### 方法一
79
+ ### 方法一:递归
80
+
81
+ 我们可以将二叉树的左指针指向 N 叉树的第一个孩子,将二叉树的右指针指向 N 叉树的下一个兄弟节点。
82
+
83
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 N 叉树的节点数量。
80
84
81
85
<!-- tabs:start -->
82
86
83
87
#### Python3
84
88
85
89
``` python
86
-
90
+ """
91
+ # Definition for a Node.
92
+ class Node:
93
+ def __init__(self, val: Optional[int] = None, children: Optional[List['Node']] = None):
94
+ self.val = val
95
+ self.children = children
96
+ """
97
+
98
+ """
99
+ # Definition for a binary tree node.
100
+ class TreeNode:
101
+ def __init__(self, x):
102
+ self.val = x
103
+ self.left = None
104
+ self.right = None
105
+ """
106
+
107
+
108
+ class Codec :
109
+ # Encodes an n-ary tree to a binary tree.
110
+ def encode (self , root : " Optional[Node]" ) -> Optional[TreeNode]:
111
+ if root is None :
112
+ return None
113
+ node = TreeNode(root.val)
114
+ if not root.children:
115
+ return node
116
+ left = self .encode(root.children[0 ])
117
+ node.left = left
118
+ for child in root.children[1 :]:
119
+ left.right = self .encode(child)
120
+ left = left.right
121
+ return node
122
+
123
+ # Decodes your binary tree to an n-ary tree.
124
+ def decode (self , data : Optional[TreeNode]) -> " Optional[Node]" :
125
+ if data is None :
126
+ return None
127
+ node = Node(data.val, [])
128
+ if data.left is None :
129
+ return node
130
+ left = data.left
131
+ while left:
132
+ node.children.append(self .decode(left))
133
+ left = left.right
134
+ return node
135
+
136
+
137
+ # Your Codec object will be instantiated and called as such:
138
+ # codec = Codec()
139
+ # codec.decode(codec.encode(root))
87
140
```
88
141
89
142
#### Java
90
143
91
144
``` java
92
-
145
+ /*
146
+ // Definition for a Node.
147
+ class Node {
148
+ public int val;
149
+ public List<Node> children;
150
+
151
+ public Node() {}
152
+
153
+ public Node(int _val) {
154
+ val = _val;
155
+ }
156
+
157
+ public Node(int _val, List<Node> _children) {
158
+ val = _val;
159
+ children = _children;
160
+ }
161
+ };
162
+ */
163
+
164
+ /**
165
+ * Definition for a binary tree node.
166
+ * public class TreeNode {
167
+ * int val;
168
+ * TreeNode left;
169
+ * TreeNode right;
170
+ * TreeNode(int x) { val = x; }
171
+ * }
172
+ */
173
+
174
+ class Codec {
175
+ // Encodes an n-ary tree to a binary tree.
176
+ public TreeNode encode (Node root ) {
177
+ if (root == null ) {
178
+ return null ;
179
+ }
180
+ TreeNode node = new TreeNode (root. val);
181
+ if (root. children == null || root. children. isEmpty()) {
182
+ return node;
183
+ }
184
+ TreeNode left = encode(root. children. get(0 ));
185
+ node. left = left;
186
+ for (int i = 1 ; i < root. children. size(); i++ ) {
187
+ left. right = encode(root. children. get(i));
188
+ left = left. right;
189
+ }
190
+ return node;
191
+ }
192
+
193
+ // Decodes your binary tree to an n-ary tree.
194
+ public Node decode (TreeNode data ) {
195
+ if (data == null ) {
196
+ return null ;
197
+ }
198
+ Node node = new Node (data. val, new ArrayList<> ());
199
+ if (data. left == null ) {
200
+ return node;
201
+ }
202
+ TreeNode left = data. left;
203
+ while (left != null ) {
204
+ node. children. add(decode(left));
205
+ left = left. right;
206
+ }
207
+ return node;
208
+ }
209
+ }
210
+
211
+ // Your Codec object will be instantiated and called as such:
212
+ // Codec codec = new Codec();
213
+ // codec.decode(codec.encode(root));
93
214
```
94
215
95
216
#### C++
96
217
97
218
``` cpp
98
-
219
+ /*
220
+ // Definition for a Node.
221
+ class Node {
222
+ public:
223
+ int val;
224
+ vector<Node*> children;
225
+
226
+ Node() {}
227
+
228
+ Node(int _val) {
229
+ val = _val;
230
+ }
231
+
232
+ Node(int _val, vector<Node*> _children) {
233
+ val = _val;
234
+ children = _children;
235
+ }
236
+ };
237
+ */
238
+
239
+ /* *
240
+ * Definition for a binary tree node.
241
+ * struct TreeNode {
242
+ * int val;
243
+ * TreeNode *left;
244
+ * TreeNode *right;
245
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
246
+ * };
247
+ */
248
+
249
+ class Codec {
250
+ public:
251
+ // Encodes an n-ary tree to a binary tree.
252
+ TreeNode* encode(Node* root) {
253
+ if (root == nullptr) {
254
+ return nullptr;
255
+ }
256
+ TreeNode* node = new TreeNode(root->val);
257
+ if (root->children.empty()) {
258
+ return node;
259
+ }
260
+ TreeNode* left = encode(root->children[ 0] );
261
+ node->left = left;
262
+ for (int i = 1; i < root->children.size(); i++) {
263
+ left->right = encode(root->children[ i] );
264
+ left = left->right;
265
+ }
266
+ return node;
267
+ }
268
+
269
+ // Decodes your binary tree to an n-ary tree.
270
+ Node* decode(TreeNode* data) {
271
+ if (data == nullptr) {
272
+ return nullptr;
273
+ }
274
+ Node* node = new Node(data->val, vector<Node*>());
275
+ if (data->left == nullptr ) {
276
+ return node;
277
+ }
278
+ TreeNode* left = data->left;
279
+ while (left != nullptr) {
280
+ node->children.push_back(decode(left));
281
+ left = left->right;
282
+ }
283
+ return node;
284
+ }
285
+ };
286
+
287
+ // Your Codec object will be instantiated and called as such:
288
+ // Codec codec;
289
+ // codec.decode(codec.encode(root));
99
290
```
100
291
101
292
#### Go
102
293
103
294
``` go
295
+ /* *
296
+ * Definition for a Node.
297
+ * type Node struct {
298
+ * Val int
299
+ * Children []*Node
300
+ * }
301
+ */
302
+
303
+ /* *
304
+ * Definition for a binary tree node.
305
+ * type TreeNode struct {
306
+ * Val int
307
+ * Left *TreeNode
308
+ * Right *TreeNode
309
+ * }
310
+ */
311
+
312
+ type Codec struct {
313
+ }
314
+
315
+ func Constructor () *Codec {
316
+ return &Codec{}
317
+ }
318
+
319
+ // Encodes an n-ary tree to a binary tree.
320
+ func (this *Codec ) encode (root *Node ) *TreeNode {
321
+ if root == nil {
322
+ return nil
323
+ }
324
+ node := &TreeNode{Val: root.Val }
325
+ if len (root.Children ) == 0 {
326
+ return node
327
+ }
328
+ left := this.encode (root.Children [0 ])
329
+ node.Left = left
330
+ for i := 1 ; i < len (root.Children ); i++ {
331
+ left.Right = this.encode (root.Children [i])
332
+ left = left.Right
333
+ }
334
+ return node
335
+ }
336
+
337
+ // Decodes your binary tree to an n-ary tree.
338
+ func (this *Codec ) decode (data *TreeNode ) *Node {
339
+ if data == nil {
340
+ return nil
341
+ }
342
+ node := &Node{Val: data.Val , Children: []*Node{}}
343
+ if data.Left == nil {
344
+ return node
345
+ }
346
+ left := data.Left
347
+ for left != nil {
348
+ node.Children = append (node.Children , this.decode (left))
349
+ left = left.Right
350
+ }
351
+ return node
352
+ }
353
+
354
+ /* *
355
+ * Your Codec object will be instantiated and called as such:
356
+ * obj := Constructor();
357
+ * bst := obj.encode(root);
358
+ * ans := obj.decode(bst);
359
+ */
360
+ ```
104
361
362
+ #### TypeScript
363
+
364
+ ``` ts
365
+ /**
366
+ * Definition for _Node.
367
+ * class _Node {
368
+ * val: number
369
+ * children: _Node[]
370
+ *
371
+ * constructor(v: number) {
372
+ * this.val = v;
373
+ * this.children = [];
374
+ * }
375
+ * }
376
+ */
377
+
378
+ /**
379
+ * Definition for a binary tree node.
380
+ * class TreeNode {
381
+ * val: number
382
+ * left: TreeNode | null
383
+ * right: TreeNode | null
384
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
385
+ * this.val = (val===undefined ? 0 : val)
386
+ * this.left = (left===undefined ? null : left)
387
+ * this.right = (right===undefined ? null : right)
388
+ * }
389
+ * }
390
+ */
391
+
392
+ class Codec {
393
+ constructor () {}
394
+
395
+ // Encodes an n-ary tree to a binary tree.
396
+ serialize(root : _Node | null ): TreeNode | null {
397
+ if (root === null ) {
398
+ return null ;
399
+ }
400
+ const node = new TreeNode (root .val );
401
+ if (root .children .length === 0 ) {
402
+ return node ;
403
+ }
404
+ let left: TreeNode | null = this .serialize (root .children [0 ]);
405
+ node .left = left ;
406
+ for (let i = 1 ; i < root .children .length ; i ++ ) {
407
+ if (left ) {
408
+ left .right = this .serialize (root .children [i ]);
409
+ left = left .right ;
410
+ }
411
+ }
412
+ return node ;
413
+ }
414
+
415
+ // Decodes your binary tree back to an n-ary tree.
416
+ deserialize(root : TreeNode | null ): _Node | null {
417
+ if (root === null ) {
418
+ return null ;
419
+ }
420
+ const node = new _Node (root .val );
421
+ if (root .left === null ) {
422
+ return node ;
423
+ }
424
+ let left: TreeNode | null = root .left ;
425
+ while (left !== null ) {
426
+ node .children .push (this .deserialize (left ));
427
+ left = left .right ;
428
+ }
429
+ return node ;
430
+ }
431
+ }
432
+
433
+ // Your Codec object will be instantiated and called as such:
434
+ // Codec codec = new Codec();
435
+ // codec.deserialize(codec.serialize(root));
105
436
```
106
437
107
438
<!-- tabs:end -->
0 commit comments