递归遍历

  1. import java.util.Stack;
  2. /**
  3. * @Description:分别用递归和非递归实现二叉树前序、中序、后序遍历
  4. * @Author: lizhouwei
  5. * @CreateDate: 2018/4/14 9:32
  6. */
  7. //前序递归
  8. public void recPreOrder(Node head) {
  9. if (head == null) {
  10. return;
  11. }
  12. System.out.print(head.value + " ");
  13. recPreOrder(head.left);
  14. recPreOrder(head.right);
  15. }
  16. //中序递归
  17. public void recInOrder(Node head) {
  18. if (head == null) {
  19. return;
  20. }
  21. recInOrder(head.left);
  22. System.out.print(head.value + " ");
  23. recInOrder(head.right);
  24. }
  25. //后序递归
  26. public void recPostOrder(Node head) {
  27. if (head == null) {
  28. return;
  29. }
  30. recPostOrder(head.left);
  31. recPostOrder(head.right);
  32. System.out.print(head.value + " ");
  33. }

前序非递归

  1. //------------非递归----------------//
  2. //前序
  3. public void preOrder(Node head) {
  4. if (head == null) {
  5. return;
  6. }
  7. Stack<Node> stack = new Stack<Node>();
  8. stack.push(head);
  9. while (!stack.isEmpty()) {
  10. head = stack.pop();
  11. System.out.print(head.value + " ");
  12. if (head.right != null) {
  13. stack.push(head.right);
  14. }
  15. if (head.left != null) {
  16. stack.push(head.left);
  17. }
  18. }
  19. }

中序非递归

  1. //中序
  2. public void inOrder(Node head) {
  3. if (head == null) {
  4. return;
  5. }
  6. Stack<Node> stack = new Stack<Node>();
  7. while (!stack.isEmpty() || head != null) {
  8. if (head != null) {
  9. stack.push(head);
  10. head = head.left;
  11. } else {
  12. head = stack.pop();
  13. System.out.print(head.value + " ");
  14. head = head.right;
  15. }
  16. }
  17. }
  18. //中序
  19. public void inOrder(Node head) {
  20. if (head == null) {
  21. return;
  22. }
  23. Stack<Node> stack = new Stack<Node>();
  24. while (!stack.isEmpty() || head != null) {
  25. while (head != null) {
  26. stack.push(head);
  27. head = head.left;
  28. }
  29. head = stack.pop();
  30. System.out.print(head.value + " ");
  31. head = head.right;
  32. }
  33. }

后序非递归

  1. //后序
  2. public void postOrder(Node head) {
  3. if (head == null) {
  4. return;
  5. }
  6. Node cur = null;
  7. Stack<Node> stack = new Stack<Node>();
  8. stack.push(head);
  9. while (!stack.isEmpty()) {
  10. cur = stack.peek();
  11. if (cur.left != null && cur.left != head && cur.right != head) {
  12. stack.push(cur.left);
  13. } else if (cur.right != null && cur.right != head) {
  14. stack.push(cur.right);
  15. } else {
  16. head = stack.pop();
  17. System.out.print(head.value + " ");
  18. }
  19. }
  20. }
  21. //后序遍历
  22. public void postOrder(Node head){
  23. if(head == null) return;
  24. Node pre = null;//当前节点的之前访问的节点
  25. Node cur;
  26. Stack<Node> stack = new Stack<>();
  27. stack.push(root);
  28. while(!stack.isEmpty()){
  29. cur = stack.peek();
  30. //当前节点是叶子节点,可以直接访问该节点
  31. //如果前一个节点不为空并且是当前节点的左孩子或者右孩子,
  32. // -->当是左孩子时说明当前节点右孩子为空,
  33. // -->当是右孩子时,说明左右孩子都访问过了,且都不为空
  34. if((cur.left == null && cur.right == null) ||
  35. (pre != null &&(pre == cur.left|| pre == cur.right)))
  36. {
  37. System.out.print(cur.val + "-->");
  38. pre = stack.pop();
  39. }
  40. else{
  41. //当前节点为栈顶元素 如果当前节点不是叶子节点,
  42. // -->在当前节点之前访问的那个节点不是当前节点的孩子,则进行压栈
  43. //先压栈右节点再压栈左节点 这样出栈时是先左后右
  44. if(cur.right != null) {
  45. stack.push(cur.right);
  46. }
  47. if(cur.left != null){
  48. stack.push(cur.left);
  49. }
  50. }
  51. }
  52. }

测试

  1. //测试
  2. public static void main(String[] args) {
  3. Chapter3_1 chapter = new Chapter3_1();
  4. int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  5. Node head = NodeUtil.generTree(arr, 0, arr.length - 1);
  6. //前序递归打印
  7. System.out.print("前序递归打印:");
  8. chapter.recPreOrder(head);
  9. System.out.println();
  10. //前序非递归打印
  11. System.out.print("前序非递归打印:");
  12. chapter.preOrder(head);
  13. System.out.println();
  14. System.out.println();
  15. //中序递归打印
  16. System.out.print("中序递归打印:");
  17. chapter.recInOrder(head);
  18. System.out.println();
  19. System.out.print("中序非递归打印:");
  20. chapter.inOrder(head);
  21. System.out.println();
  22. System.out.println();
  23. //后序递归打印
  24. System.out.print("后序递归打印:");
  25. chapter.recPostOrder(head);
  26. System.out.println();
  27. System.out.print("后序非递归打印:");
  28. chapter.postOrder(head);
  29. }