1. algorithm

116. 填充每个节点的下一个右侧节点指针

  1. class Solution {
  2. public:
  3. struct Node_floor {
  4. Node *node;
  5. int floor;
  6. Node_floor() :node(NULL), floor(0) {}
  7. Node_floor(Node *n) :node(n), floor(0) {}
  8. Node_floor(Node *n, int f) :node(n), floor(f) {}
  9. };
  10. Node* connect(Node* root)
  11. {
  12. #if 1
  13. if(!root) return NULL;
  14. // vector<Node_floor> q;
  15. queue<Node_floor> q;
  16. int f = 0;
  17. q.push(Node_floor(root, f));
  18. while(!q.empty()) {
  19. Node_floor tmp = q.front();
  20. if(tmp.node) {
  21. if(tmp.node->left) {
  22. tmp.node->left->next = tmp.node->right;
  23. q.push( Node_floor(tmp.node->left, tmp.floor + 1) );
  24. }
  25. if(tmp.node->right) {
  26. q.push( Node_floor(tmp.node->right, tmp.floor + 1) );
  27. }
  28. }
  29. q.pop();
  30. if(!q.empty()) {
  31. Node_floor n = q.front();
  32. if(tmp.floor == n.floor) {
  33. tmp.node->next = n.node;
  34. } else {
  35. tmp.node->next = NULL;
  36. }
  37. } else {
  38. tmp.node->next = NULL;
  39. }
  40. }
  41. #else
  42. if(!root) return NULL;
  43. Node *left = root->left;
  44. Node *right = root->right;
  45. while(left) {
  46. left->next = right;
  47. left = left->right;
  48. right = right->left;
  49. }
  50. connect(root->left);
  51. connect(root->right);
  52. #endif
  53. return root;
  54. }
  55. };

层序遍历,使用queue在每个node上打了一个层的标签,整体效率还好,主要依赖于queue模板效率。
另一种参考了题解的方法:拉拉链,实际是将树的所有子树二分。然后每次都以该节点为轴将下面所有轴两侧的节点连接起来。
这种层序遍历的方法直接在下一题117就ok了,但是另一种拉拉链则只能在这种非常规整的情况下使用。

297. 二叉树的序列化与反序列化

  1. class Codec {
  2. public:
  3. // Encodes a tree to a single string.
  4. string serialize(TreeNode* root) {
  5. queue<TreeNode*> q;
  6. if(root == NULL) {
  7. return "null";
  8. }
  9. string ret = "";
  10. char buf[10];
  11. q.push(root);
  12. while(!q.empty()) {
  13. TreeNode* t = q.front();
  14. if(t) {
  15. snprintf(buf, sizeof(buf), "%d", t->val);
  16. ret += buf;
  17. ret += ",";
  18. q.push(t->left);
  19. q.push(t->right);
  20. } else {
  21. ret += "null,";
  22. }
  23. q.pop();
  24. }
  25. string::size_type n;
  26. while((n = ret.find("null,", ret.size() - 5)) != string::npos) {
  27. ret.erase(ret.size() - 5);
  28. }
  29. ret.erase(ret.size() - 1);
  30. // cout << ret << endl;
  31. return ret;
  32. }
  33. TreeNode *parse(string &data)
  34. {
  35. if(data.empty()) {
  36. return NULL;
  37. }
  38. TreeNode *ret = NULL;
  39. string::size_type n = data.find(",");
  40. if(n != string::npos) {
  41. string tmp = data.substr(0, n);
  42. if(tmp == "null") {
  43. data.erase(0, n + 1);
  44. return NULL;
  45. }
  46. ret = new TreeNode(atoi(tmp.c_str()));
  47. // data.erase(0, n + 1);
  48. data = data.substr(n + 1);
  49. } else {
  50. if(data == "null") {
  51. return NULL;
  52. }
  53. ret = new TreeNode(atoi(data.c_str()));
  54. data.clear();
  55. }
  56. return ret;
  57. }
  58. // Decodes your encoded data to tree.
  59. TreeNode* deserialize(string data)
  60. {
  61. if(data.compare(0, 4, "null") == 0) {
  62. return NULL;
  63. }
  64. queue<TreeNode*> nodes;
  65. TreeNode *root = parse(data);
  66. TreeNode *p;
  67. nodes.push(root);
  68. while(!nodes.empty()) {
  69. p = nodes.front();
  70. nodes.pop();
  71. if(p) {
  72. p->left = parse(data);
  73. nodes.push(p->left);
  74. p->right = parse(data);
  75. nodes.push(p->right);
  76. }
  77. }
  78. return root;
  79. }
  80. };

2. review

3. tips

4. share