二叉树遍历

第10节.pptx

题目1:递归方式实现二叉树先序、中序、后序遍历

  1. // 先序打印 头-左-右
  2. public static void pre(TreeNode head) {
  3. if (head == null) {
  4. return;
  5. }
  6. System.out.print(head.val + " ");
  7. pre(head.left);
  8. pre(head.right);
  9. }
  10. // 中序打印 左-头-右
  11. public static void in(TreeNode head) {
  12. if (head == null) {
  13. return;
  14. }
  15. in(head.left);
  16. System.out.print(head.val + " ");
  17. in(head.right);
  18. }
  19. // 后序打印 左-右-头
  20. public static void pos(TreeNode head) {
  21. if (head == null) {
  22. return;
  23. }
  24. pos(head.left);
  25. pos(head.right);
  26. System.out.print(head.val + " ");
  27. }

题目2 非递归方式实现二叉树先序、中序、后序遍历

  1. // 先序 头-左-右
  2. public static void pre(TreeNode head) {
  3. if (head == null) {
  4. return;
  5. }
  6. Stack<TreeNode> stack = new Stack<>();
  7. stack.push(head);
  8. while (!stack.empty()) {
  9. head = stack.pop();
  10. if (head.right != null) {
  11. stack.push(head.right);
  12. }
  13. if (head.left != null) {
  14. stack.push(head.left);
  15. }
  16. System.out.print(head.val + " ");
  17. }
  18. }
  19. // 中序 左 头 右
  20. public static void in1(TreeNode head) {
  21. if (head == null) {
  22. return;
  23. }
  24. Stack<TreeNode> stack = new Stack<>();
  25. pushLeft(stack, head);
  26. while (!stack.empty()) {
  27. head = stack.pop();
  28. System.out.print(head.val + " ");
  29. if (head.right != null) {
  30. pushLeft(stack, head.right);
  31. }
  32. }
  33. }
  34. private static void pushLeft(Stack<TreeNode> stack, TreeNode head) {
  35. stack.push(head);
  36. while (head.left != null) {
  37. stack.push(head.left);
  38. head = head.left;
  39. }
  40. }
  41. public static void in2(Node cur) {
  42. System.out.print("in-order: ");
  43. if (cur != null) {
  44. Stack<Node> stack = new Stack<Node>();
  45. while (!stack.isEmpty() || cur != null) {
  46. if (cur != null) {
  47. stack.push(cur);
  48. cur = cur.left;
  49. } else {
  50. cur = stack.pop();
  51. System.out.print(cur.value + " ");
  52. cur = cur.right;
  53. }
  54. }
  55. }
  56. System.out.println();
  57. }
  58. // 后序 左 右 头
  59. public static void pos1(TreeNode head) {
  60. if (head == null) {
  61. return;
  62. }
  63. Stack<TreeNode> s1 = new Stack<>();
  64. Stack<TreeNode> s2 = new Stack<>();
  65. s1.push(head);
  66. while (!s1.isEmpty()) {
  67. // 头 右 左
  68. head = s1.pop();
  69. s2.push(head);
  70. if (head.left != null) {
  71. s1.push(head.left);
  72. }
  73. if (head.right != null) {
  74. s1.push(head.right);
  75. }
  76. }
  77. // 左 右 头
  78. while (!s2.isEmpty()) {
  79. System.out.print(s2.pop().val + " ");
  80. }
  81. System.out.println();
  82. }
  83. public static void pos2(TreeNode head) {
  84. if (head == null) {
  85. return;
  86. }
  87. Stack<TreeNode> stack = new Stack<>();
  88. stack.push(head);
  89. TreeNode cur = null;
  90. while (!stack.isEmpty()) {
  91. cur = stack.peek();
  92. if (cur.left != null && head != cur.left && head != cur.right) {
  93. stack.push(cur.left);
  94. } else if (cur.right != null && head != cur.right) {
  95. stack.push(cur.right);
  96. } else {
  97. System.out.print(stack.pop().val + " ");
  98. head = cur;
  99. }
  100. }
  101. System.out.println();
  102. }

附加题

给一个二叉树的先序遍历结果集,其中一个节点x
给这个二叉树的后序遍历结果集,其中一个节点x
求证明:先序遍历结果集中x前面的节点与后序遍历结果集中x后面的节点的交集是x全部的祖先节点、ppt
证明:

  1. 先序遍历结果集,x的全部祖先节点在x前面;后序遍历结果集,x全部祖先节点在
  2. x为左边节点
    1. x为最左边节点

先序结果集,x前面只有x的祖先节点,因此和后序遍历结果集的交集为全部祖先节点

  1. x为中间左边节点

先序结果集,x前面包含x祖先节点和祖先左子树节点;
后序结果集,x后面包含x主线节点和祖先右子树节点
因此,交集只有全部祖先节点

  1. x为右边节点

    1. x为最右边节点

    后序结果集,x后面只有x的祖先节点,因此和先序遍历结果集的交集为全部祖先节点

    1. x为中间有边节点

先序结果集,x前面包含x祖先节点和祖先左子树节点;
后序结果集,x后面包含x祖先节点和祖先右子树节点;
因此,交集只有全部祖先节点