知识点:

遍历二叉树,可有3+1种方法:先序、中序、后序和层次法。
以下前三种方法从根部开始逆时针方向绕过各结点,形成一条蜿蜒“足迹”。

(1)先序法(又称先根法)
先序遍历:根,左子树,右子树
遍历的结果:A,B,C
遍历的足迹:沿途经过各结点的“左部”

(2)中序法(又称中根法)
中序遍历:左子树,根,右子树
遍历的结果:B,A,C
遍历的足迹:沿途经过各结点的“下部”

(3)后序法(又称后根法)
后序遍历:左子树,右子树,根
遍历的结果:B,C,A
遍历的足迹:沿途经过各结点的“右部”

(4)层次法
层次遍历:从根开始,层次自上到下,同层结点自左至右进行。
遍历的结果:A,B,C
遍历的足迹:第一层A,第二层B,C


代码实现:
1642499304(1).png

递归方法

  1. //递归序 每个节点都会遍历三次 : 124666477742555213888399931
  2. public static void f(TreeNode root){
  3. if(root==null) return;
  4. System.out.print(root.val);
  5. f(root.left);
  6. System.out.print(root.val);
  7. f(root.right);
  8. System.out.print(root.val);
  9. }
  10. //先序遍历 取递归序 每个节点出现第一次 124675389 --深度遍历
  11. public static void preorder(TreeNode root){
  12. if(root==null) return;
  13. System.out.print(root.val);
  14. preorder(root.left);
  15. preorder(root.right);
  16. }
  17. //中序遍历 取递归序 每个节点出现第二次647251839
  18. public static void midorder(TreeNode root){
  19. if(root==null) return;
  20. midorder(root.left);
  21. System.out.print(root.val);
  22. midorder(root.right);
  23. }
  24. //后序遍历 取递归序 每个节点出现第三次 674528931
  25. public static void posorder(TreeNode root){
  26. if(root==null) return;
  27. posorder(root.left);
  28. posorder(root.right);
  29. System.out.print(root.val);
  30. }

非递归方法

  1. //非递归 先序 构建一个栈 存入头结点 当栈不为空 输出栈顶节点 当这个节点有左右节点 不为空 右左存储
  2. public static void preorder1(TreeNode root){
  3. Stack<TreeNode> s =new Stack<>();
  4. s.push(root);
  5. while (!s.empty()){
  6. TreeNode t =s.pop();
  7. System.out.print(t.val);
  8. if(t.right!=null) s.push(t.right);
  9. if(t.left!=null) s.push(t.left);
  10. }
  11. }
  12. //非递归 中序 构建一个栈 先将树的所有左节点压入 (根节点也算) 遇到左节点为空 开始进行出栈 并且出栈节点要是有右节点就压入
  13. public static void midoder1(TreeNode root){
  14. Stack<TreeNode> s =new Stack<>();
  15. while (!s.empty()|| root!=null){
  16. if(root!=null){
  17. s.push(root);
  18. root=root.left;
  19. }
  20. else {
  21. root =s.pop();
  22. System.out.println(root.val);
  23. root = root.right;
  24. }
  25. }
  26. }
  27. //非递归 后续序 构建两个栈 存入头结点 当栈不为空 输出栈顶节点到第二个栈 当这个节点有左右节点 不为空 左右存储 先序 头左右-》 头右左 调换左右放进去的顺序 最后结果进行反转
  28. public static void posorder1(TreeNode root){
  29. Stack<TreeNode> s =new Stack<>();
  30. s.push(root);
  31. List<Integer> list =new ArrayList<>();
  32. while (!s.empty()){
  33. TreeNode t =s.pop();
  34. list.add(t.val);
  35. if(t.left!=null) s.push(t.left);
  36. if(t.right!=null) s.push(t.right);
  37. }
  38. Collections.reverse(list);
  39. for (Integer integer : list) {
  40. System.out.print(integer);
  41. }
  42. }
  43. //非递归层次遍历 将每一层的输出 从左到右 宽度遍历
  44. public static void preorder2(TreeNode root){
  45. Queue <TreeNode> q =new LinkedList<>();
  46. q.offer(root);
  47. int n = q.size();
  48. while (!q.isEmpty()){
  49. if(q.size()>n) n=q.size();//求宽度
  50. for (int i = q.size(); i >0; i--) {
  51. TreeNode t =q.poll();
  52. System.out.print(t.val);
  53. if(t.left!=null) q.add(t.left);
  54. if(t.right!=null) q.add(t.right);
  55. }
  56. }
  57. System.out.println(n);
  58. }