|
27 | 27 |
|
28 | 28 | <!-- 这里可写通用的实现逻辑 -->
|
29 | 29 |
|
30 |
| -层次遍历解决。 |
| 30 | +**方法一:层序遍历** |
| 31 | + |
| 32 | +**方法二:前序遍历** |
| 33 | + |
| 34 | +当二叉树的前中后序列不包含叶子节点时需要前中、前后、中后三种组合方式之一才能确定一颗二叉树,但当前序和后序遍历序列中包含叶子节点时,可以仅通过前序或后序遍历序列构建一颗二叉树。 |
| 35 | + |
| 36 | +在前序遍历序列化时,我们以任意特殊字符表示叶子节点,返回序列化后的字符串;反序列化时对序列化字符串根据分隔符进行切分后使用列表的第一个元素作为二叉树的根节点,然后利用列表的其他元素递归生成左右子树即可。 |
| 37 | + |
| 38 | +**方法三:后序遍历** |
| 39 | + |
| 40 | +在后序遍历序列化时,我们以任意特殊字符表示叶子节点,返回序列化后的字符串;反序列化时对序列化字符串根据分隔符进行切分后使用列表的最后一个元素作为二叉树的根节点,然后利用列表的其他元素递归生成左右子树即可。 |
31 | 41 |
|
32 | 42 | <!-- tabs:start -->
|
33 | 43 |
|
@@ -343,6 +353,195 @@ public class Codec {
|
343 | 353 | // codec.deserialize(codec.serialize(root));
|
344 | 354 | ```
|
345 | 355 |
|
| 356 | +### **C++** |
| 357 | + |
| 358 | +层序遍历: |
| 359 | + |
| 360 | +```cpp |
| 361 | +/** |
| 362 | + * Definition for a binary tree node. |
| 363 | + * struct TreeNode { |
| 364 | + * int val; |
| 365 | + * TreeNode *left; |
| 366 | + * TreeNode *right; |
| 367 | + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} |
| 368 | + * }; |
| 369 | + */ |
| 370 | +class Codec { |
| 371 | +public: |
| 372 | + string empty = "#"; |
| 373 | + string sep = ","; |
| 374 | + // Encodes a tree to a single string. |
| 375 | + string serialize(TreeNode* root) { |
| 376 | + if (!root) return ""; |
| 377 | + string res = ""; |
| 378 | + queue<TreeNode*> q; |
| 379 | + q.push(root); |
| 380 | + while (!q.empty()) { |
| 381 | + TreeNode* node = q.front(); |
| 382 | + q.pop(); |
| 383 | + if (!node) { |
| 384 | + res += empty + sep; |
| 385 | + continue; |
| 386 | + } |
| 387 | + res += to_string(node->val) + sep; |
| 388 | + q.push(node->left); |
| 389 | + q.push(node->right); |
| 390 | + } |
| 391 | + return res; |
| 392 | + } |
| 393 | + |
| 394 | + // Decodes your encoded data to tree. |
| 395 | + TreeNode* deserialize(string data) { |
| 396 | + if (data.empty()) return nullptr; |
| 397 | + vector<string> nodes; |
| 398 | + size_t pos = 0; |
| 399 | + string node; |
| 400 | + while ((pos = data.find(sep)) != string::npos) { |
| 401 | + node = data.substr(0, pos); |
| 402 | + nodes.push_back(node); |
| 403 | + data.erase(0, pos + sep.length()); |
| 404 | + } |
| 405 | + queue<TreeNode*> q; |
| 406 | + TreeNode* root = new TreeNode(stoi(nodes[0])); |
| 407 | + q.push(root); |
| 408 | + |
| 409 | + for (size_t i = 1; i < nodes.size();) { |
| 410 | + TreeNode* front = q.front(); |
| 411 | + q.pop(); |
| 412 | + // 左子树 |
| 413 | + node = nodes[i++]; |
| 414 | + if (node != empty) { |
| 415 | + front->left = new TreeNode(stoi(node)); |
| 416 | + q.push(front->left); |
| 417 | + } |
| 418 | + // 右子树 |
| 419 | + node = nodes[i++]; |
| 420 | + if (node != empty) { |
| 421 | + front->right = new TreeNode(stoi(node)); |
| 422 | + q.push(front->right); |
| 423 | + } |
| 424 | + } |
| 425 | + return root; |
| 426 | + } |
| 427 | +}; |
| 428 | +// Your Codec object will be instantiated and called as such: |
| 429 | +// Codec codec; |
| 430 | +// codec.deserialize(codec.serialize(root)); |
| 431 | +``` |
| 432 | + |
| 433 | +前序遍历: |
| 434 | + |
| 435 | +```cpp |
| 436 | +/** |
| 437 | + * Definition for a binary tree node. |
| 438 | + * struct TreeNode { |
| 439 | + * int val; |
| 440 | + * TreeNode *left; |
| 441 | + * TreeNode *right; |
| 442 | + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} |
| 443 | + * }; |
| 444 | + */ |
| 445 | +class Codec { |
| 446 | +public: |
| 447 | + string empty = "#"; |
| 448 | + string sep = ","; |
| 449 | + // Encodes a tree to a single string. |
| 450 | + string serialize(TreeNode* root) { |
| 451 | + if (!root) return empty + sep; |
| 452 | + string res = to_string(root->val) + sep; |
| 453 | + res += serialize(root->left); |
| 454 | + res += serialize(root->right); |
| 455 | + return res; |
| 456 | + } |
| 457 | + |
| 458 | + // Decodes your encoded data to tree. |
| 459 | + TreeNode* deserialize(string data) { |
| 460 | + list<string> nodes; |
| 461 | + size_t pos = 0; |
| 462 | + string node; |
| 463 | + while ((pos = data.find(sep)) != string::npos) { |
| 464 | + node = data.substr(0, pos); |
| 465 | + nodes.push_back(node); |
| 466 | + data.erase(0, pos + sep.length()); |
| 467 | + } |
| 468 | + return deserialize(nodes); |
| 469 | + } |
| 470 | + |
| 471 | + TreeNode* deserialize(list<string>& data) { |
| 472 | + if (data.empty()) return nullptr; |
| 473 | + string first = data.front(); |
| 474 | + data.pop_front(); |
| 475 | + if (first == empty) return nullptr; |
| 476 | + TreeNode* root = new TreeNode(stoi(first)); |
| 477 | + root->left = deserialize(data); |
| 478 | + root->right = deserialize(data); |
| 479 | + return root; |
| 480 | + } |
| 481 | +}; |
| 482 | + |
| 483 | +// Your Codec object will be instantiated and called as such: |
| 484 | +// Codec codec; |
| 485 | +// codec.deserialize(codec.serialize(root)); |
| 486 | +``` |
| 487 | +
|
| 488 | +后序遍历: |
| 489 | +
|
| 490 | +```cpp |
| 491 | +/** |
| 492 | + * Definition for a binary tree node. |
| 493 | + * struct TreeNode { |
| 494 | + * int val; |
| 495 | + * TreeNode *left; |
| 496 | + * TreeNode *right; |
| 497 | + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} |
| 498 | + * }; |
| 499 | + */ |
| 500 | +class Codec { |
| 501 | +public: |
| 502 | + string empty = "#"; |
| 503 | + string sep = ","; |
| 504 | + // Encodes a tree to a single string. |
| 505 | + string serialize(TreeNode* root) { |
| 506 | + if (!root) return empty + sep; |
| 507 | + string res = ""; |
| 508 | + res += serialize(root->left); |
| 509 | + res += serialize(root->right); |
| 510 | + res += to_string(root->val) + sep; |
| 511 | + return res; |
| 512 | + } |
| 513 | +
|
| 514 | + // Decodes your encoded data to tree. |
| 515 | + TreeNode* deserialize(string data) { |
| 516 | + vector<string> nodes; |
| 517 | + size_t pos = 0; |
| 518 | + string node; |
| 519 | + while ((pos = data.find(sep)) != string::npos) { |
| 520 | + node = data.substr(0, pos); |
| 521 | + nodes.push_back(node); |
| 522 | + data.erase(0, pos + sep.length()); |
| 523 | + } |
| 524 | + return deserialize(nodes); |
| 525 | + } |
| 526 | +
|
| 527 | + TreeNode* deserialize(vector<string>& nodes) { |
| 528 | + if (nodes.empty()) return nullptr; |
| 529 | + string front = nodes.back(); |
| 530 | + nodes.pop_back(); |
| 531 | + if (front == empty) return nullptr; |
| 532 | + TreeNode* root = new TreeNode(stoi(front)); |
| 533 | + // 先构造右子树,后构造左子树 |
| 534 | + root->right = deserialize(nodes); |
| 535 | + root->left = deserialize(nodes); |
| 536 | + return root; |
| 537 | + } |
| 538 | +}; |
| 539 | +
|
| 540 | +// Your Codec object will be instantiated and called as such: |
| 541 | +// Codec codec; |
| 542 | +// codec.deserialize(codec.serialize(root)); |
| 543 | +``` |
| 544 | + |
346 | 545 | ### **...**
|
347 | 546 |
|
348 | 547 | ```
|
|
0 commit comments