1. // 递归版本前序遍历
    2. public static void preOrder1(Node root){
    3. if(root == null){
    4. return ;
    5. }
    6. System.out.print( root.value+" ");
    7. preOrder1(root.left);
    8. preOrder1(root.right);
    9. }
    10. // 非递归版本前序遍历 头左右
    11. // 1)把头节点压入栈中,从栈中弹出一个节点cur
    12. // 2)打印cur
    13. // 3)先右再左入栈(如果有的话)
    14. // 4)重复操作
    15. public static void preOrder2(Node head){
    16. if(head == null){
    17. return;
    18. }
    19. Stack<Node> stack = new Stack<>();
    20. stack.add(head);
    21. while(!stack.isEmpty()){
    22. Node pop = stack.pop();
    23. System.out.print(pop.value + " ");
    24. if(pop.right != null){
    25. stack.push(pop.right);
    26. }
    27. if(pop.left != null){
    28. stack.push(pop.left);
    29. }
    30. }
    31. }
    32. // 归版本后序遍历 左右头
    33. public static void laterOrder1(Node head){
    34. if(head == null){
    35. return;
    36. }
    37. laterOrder1(head.left);
    38. laterOrder1(head.right);
    39. System.out.print(head.value+" ");
    40. }
    41. // 非递归版本后序遍历 左右头
    42. // 1)把头节点压入栈中,从栈中弹出一个节点cur,
    43. // 2)把cur放入收集栈中
    44. // 3)先左再右入栈(如果有的话)
    45. // 4)重复操作 收集完打印
    46. // 头左右→头右左→左右头
    47. public static void laterOrder2(Node head){
    48. if(head == null){
    49. return;
    50. }
    51. Stack<Node> stack1 = new Stack<>();
    52. Stack<Node> stack2 = new Stack<>();
    53. stack1.push(head);
    54. while(!stack1.isEmpty()){
    55. Node pop = stack1.pop();
    56. stack2.push(pop);
    57. if(pop.left != null){
    58. stack1.push(pop.left);
    59. }
    60. if(pop.right != null){
    61. stack1.push(pop.right);
    62. }
    63. }
    64. while(!stack2.isEmpty()){
    65. Node pop = stack2.pop();
    66. System.out.print(pop.value+" ");
    67. }
    68. }
    69. // 递归版本中序遍历 左头右
    70. public static void MidOrder1(Node head){
    71. if(head == null ){
    72. return;
    73. }
    74. MidOrder1(head.left);
    75. System.out.print(head.value + " ");
    76. MidOrder1(head.right);
    77. }
    78. // 非递归版本中序遍历 左头右
    79. // 每棵子树整棵树左边界进栈,依次弹出的过程中打印,对弹出节点的右树做重复操作压左边界进栈。
    80. // 左头右(分解)
    81. // 左头右....
    82. public static void MidOrder2(Node head){
    83. if(head == null ){
    84. return;
    85. }
    86. Stack<Node> stack = new Stack<>();
    87. while(!stack.isEmpty() || head != null){
    88. // 一直压左树,为空出栈打印,并对右树做同样操作。
    89. if(head != null){
    90. stack.push(head);
    91. head = head.left;
    92. }else {
    93. head = stack.pop();
    94. System.out.print(head.value + " ");
    95. head = head.right;
    96. }
    97. }
    98. }
    99. // 树的宽度遍历
    100. public static void WidthOrder(Node head){
    101. if(head == null){
    102. return;
    103. }
    104. Queue<Node> queue = new LinkedList<>();
    105. queue.add(head);
    106. while(!queue.isEmpty()){
    107. Node poll = queue.poll();
    108. System.out.print(poll.value + " ");
    109. if(poll.left != null){
    110. queue.add(poll.left);
    111. }
    112. if(poll.right != null){
    113. queue.add(poll.right);
    114. }
    115. }
    116. }