(PAT 1102) Invert a Binary Tree(反转二叉树+寻找二叉树的根结点)

冷不防 2022-04-23 03:54 229阅读 0赞

The following is from Max Howell @twitter:

  1. Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off.

Now it’s your turn to prove that YOU CAN invert a binary tree!

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree — and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node from 0 to N−1, and gives the indices of the left and right children of the node. If the child does not exist, a - will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each test case, print in the first line the level-order, and then in the second line the in-order traversal sequences of the inverted tree. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:

  1. 8
  2. 1 -
  3. - -
  4. 0 -
  5. 2 7
  6. - -
  7. - -
  8. 5 -
  9. 4 6

Sample Output:

  1. 3 7 2 6 4 0 5 1
  2. 6 5 7 4 3 2 0 1

解题思路:

利用前序遍历,遍历到每一个结点时,交换该结点的左右子树即可

  1. void invertBinaryTree(int root) {
  2. if (root == -1) return;
  3. swap(BTree[root].lchild, BTree[root].rchild);
  4. invertBinaryTree(BTree[root].lchild);
  5. invertBinaryTree(BTree[root].rchild);
  6. }

这里的坑点是怎样寻找二叉树的根结点: 根据性质,二叉树根节点是只有出度没有入度的,所以在插入节点时,给每个子节点上一个不是根节点的标记,最后没有被上标记的那个就是根节点

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <queue>
  5. using namespace std;
  6. const int MAXN = 100001;
  7. int N;
  8. struct sNode {
  9. int lchild = -1, rchild = -1;
  10. bool notRoot = false;
  11. }BTree[MAXN];
  12. void invertBinaryTree(int root) {
  13. if (root == -1) return;
  14. swap(BTree[root].lchild, BTree[root].rchild);
  15. invertBinaryTree(BTree[root].lchild);
  16. invertBinaryTree(BTree[root].rchild);
  17. }
  18. int num1 = 0;
  19. void inorder(int root) {
  20. if (BTree[root].lchild != -1) {
  21. inorder(BTree[root].lchild);
  22. }
  23. cout << root;
  24. num1++;
  25. if (num1 < N) cout << " ";
  26. if (BTree[root].rchild != -1) {
  27. inorder(BTree[root].rchild);
  28. }
  29. }
  30. int num2 = 0;
  31. void LayerOrder(int root) {
  32. queue<int> BFS_QUeue;
  33. BFS_QUeue.push(root);
  34. while (!BFS_QUeue.empty()) {
  35. cout << BFS_QUeue.front();
  36. int curnode = BFS_QUeue.front();
  37. num2++;
  38. if (num2 < N) cout << " ";
  39. BFS_QUeue.pop();
  40. if (BTree[curnode].lchild != -1) {
  41. BFS_QUeue.push(BTree[curnode].lchild);
  42. }
  43. if (BTree[curnode].rchild != -1) {
  44. BFS_QUeue.push(BTree[curnode].rchild);
  45. }
  46. }
  47. }
  48. int main() {
  49. scanf("%d", &N);
  50. for (int i = 0; i < N; ++i) {
  51. char lc, rc;
  52. scanf("%*c%c %c", &lc, &rc);
  53. if (lc == '-' && rc == '-') {
  54. BTree[i].lchild = -1;
  55. BTree[i].rchild = -1;
  56. BTree[BTree[i].lchild].notRoot = true;
  57. BTree[BTree[i].rchild].notRoot = true;
  58. }
  59. else if (lc == '-' || rc == '-') {
  60. if (lc == '-') {
  61. BTree[i].lchild = -1;
  62. BTree[i].rchild = rc - '0';
  63. BTree[BTree[i].rchild].notRoot = true;
  64. }
  65. else if(rc == '-'){
  66. BTree[i].lchild = lc - '0';
  67. BTree[i].rchild = -1;
  68. BTree[BTree[i].lchild].notRoot = true;
  69. }
  70. }
  71. else {
  72. BTree[i].lchild = lc - '0';
  73. BTree[i].rchild = rc - '0';
  74. BTree[BTree[i].lchild].notRoot = true;
  75. BTree[BTree[i].rchild].notRoot = true;
  76. }
  77. }
  78. //确立根结点
  79. int droot = 0;
  80. for (int i = 0; i < N; ++i) {
  81. if (BTree[i].notRoot == false) {
  82. droot = i;
  83. break;
  84. }
  85. }
  86. //反转二叉树
  87. invertBinaryTree(droot);
  88. LayerOrder(droot);
  89. cout << endl;
  90. inorder(droot);
  91. system("PAUSE");
  92. return 0;
  93. }

发表评论

表情:
评论列表 (有 0 条评论,229人围观)

还没有评论,来说两句吧...

相关阅读