剑指offer之中序打印二叉树(非递归实现)

举报
chenyu 发表于 2021/07/27 01:13:47 2021/07/27
【摘要】 1 问题 中序打印二叉树(非递归实现),比如二叉树如下 /* 2 * 3 5 * 1 4 2 3 * 3 2 1 5 1 4 2 3 中序:按左中右来打印二叉树,结果如下 312314521245233                 &nbsp...

1 问题

中序打印二叉树(非递归实现),比如二叉树如下


  
  1. /* 2
  2. * 3 5
  3. * 1 4 2 3
  4. * 3 2 1 5 1 4 2 3

中序:按左中右来打印二叉树,结果如下


  
  1. 3
  2. 1
  3. 2
  4. 3
  5. 1
  6. 4
  7. 5
  8. 2
  9. 1
  10. 2
  11. 4
  12. 5
  13. 2
  14. 3
  15. 3

 

 

 

 

 

 

 

 

 

2 代码实现


  
  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4. typedef struct Node
  5. {
  6. int value;
  7. struct Node* left;
  8. struct Node* right;
  9. } Node;
  10. void center_print(Node *head)
  11. {
  12. if (head == NULL)
  13. {
  14. std::cout << "head is NULL" << std::endl;
  15. return;
  16. }
  17. std::stack<Node *> stack;
  18. Node *p = head;
  19. while (p != NULL || !stack.empty())
  20. {
  21. while (p != NULL)
  22. {
  23. stack.push(p);
  24. p = p->left;
  25. }
  26. if (!stack.empty())
  27. {
  28. p = stack.top();
  29. std::cout << p->value << std::endl;
  30. stack.pop();
  31. p = p->right;
  32. }
  33. }
  34. }
  35. int main()
  36. {
  37. /* 2
  38. * 3 5
  39. * 1 4 2 3
  40. * 3 2 1 5 1 4 2 3
  41. */
  42. Node head1, node1, node2, node3, node4, node5, node6;
  43. Node node7, node8, node9, node10, node11, node12, node13, node14;
  44. head1.value = 2;
  45. node1.value = 3;
  46. node2.value = 5;
  47. node3.value = 1;
  48. node4.value = 4;
  49. node5.value = 2;
  50. node6.value = 3;
  51. node7.value = 3;
  52. node8.value = 2;
  53. node9.value = 1;
  54. node10.value = 5;
  55. node11.value = 1;
  56. node12.value = 4;
  57. node13.value = 2;
  58. node14.value = 3;
  59. head1.left = &node1;
  60. head1.right = &node2;
  61. node1.left = &node3;
  62. node1.right = &node4;
  63. node2.left = &node5;
  64. node2.right = &node6;
  65. node3.left = &node7;
  66. node3.right = &node8;
  67. node4.left = &node9;
  68. node4.right = &node10;
  69. node5.left = &node11;
  70. node5.right = &node12;
  71. node6.left = &node13;
  72. node6.right = &node14;
  73. node7.left = NULL;
  74. node7.right = NULL;
  75. node8.left = NULL;
  76. node8.right = NULL;
  77. node9.left = NULL;
  78. node9.right = NULL;
  79. node10.left = NULL;
  80. node10.right = NULL;
  81. node11.left = NULL;
  82. node11.right = NULL;
  83. node12.left = NULL;
  84. node12.right = NULL;
  85. node13.left = NULL;
  86. node13.right = NULL;
  87. node14.left = NULL;
  88. node14.right = NULL;
  89. center_print(&head1);
  90. return 0;
  91. }

 

 

 

 

 

 

 

 

3 运行结果


  
  1. 2
  2. 3
  3. 1
  4. 3
  5. 2
  6. 4
  7. 1
  8. 5
  9. 5
  10. 2
  11. 1
  12. 4
  13. 3
  14. 2
  15. 3

 

文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。

原文链接:chenyu.blog.csdn.net/article/details/90350060

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。