算法day6 二叉树的遍历与求最大宽度。

1.二叉树的基本结构:节点

public class Node {
    public int value;
    public Node left;
    public Node right;

    public Node(int data) {
        this.value = data;
    }
}

2.二叉树的深度优先遍历:先序 中序 后序遍历

2.1.二叉树递归遍历方式的本质是利用递归的逐级返回,简称递归序

//递归序的本质,每一个节点必然来到三次,如何递归遍历一棵树,只需要在不同的位置进行值的展示
    public static void f(Node head) {
        //1.第一次来到该节点,先序遍历
        if (head == null)
            return;
        f(head.left);
        //2.第二次来到该节点,中序遍历
        f(head.right);
        //3.第三次来到该节点,后序遍历
    }

2.2 递归形式的先序 中序 后序遍历

//先序遍历,只在第一次到达该节点时打印
    public static void preOrderRecur(Node head) {
        if (head == null)
            return;
        System.out.print(head.value + " ");
        preOrderRecur(head.left);
        preOrderRecur(head.right);
    }

    //中序遍历,只在第二次到达该节点时打印
    public static void inOrderRecur(Node head) {
        if (head == null)
            return;
        inOrderRecur(head.left);
        System.out.print(head.value + " ");
        inOrderRecur(head.right);
    }

    //后序遍历,只在第三次到达该节点时打印
    public static void posOrderRecur(Node head) {
        if (head == null)
            return;
        posOrderRecur(head.left);
        posOrderRecur(head.right);
        System.out.print(head.value + " ");
    }

3.非递归形式实现二叉树的遍历

3.1  非递归行为实现先序遍历         思路:首先准备一个栈,起步将头节点扔到栈里面去。 然后:1.将栈顶元素弹出,进行处理(打印) 2.先将该元素的右孩子放入栈中,再将该元素的左孩子放入栈中,如果没有就跳过 3.重复1和2,直到栈空

public static void preOrderUnRecur(Node head) {
        System.out.print("pre-order: ");
        if (head != null) {
            Stack<Node> stack = new Stack<>();
            stack.add(head);
            while (!stack.isEmpty()) {
                head = stack.pop();
                System.out.print(head.value + " ");
                if (head.right != null)
                    stack.push(head.right);
                if (head.left != null)
                    stack.push(head.left);

            }
        }
        System.out.println();
    }

3.2  非递归行为实现后序遍历        思路:首先准备两个个栈,一个容器栈,一个收集栈,起步将头节点扔到容器栈里面去. 然后:1.弹出容器栈的栈顶元素扔到收集栈中去 2.将该元素先左孩子后右孩子放到容器栈中去,如果没有则跳过 3.重复1和2,直到栈空,然后将收集栈中的节点依次到出来,就是后序遍历

 public static void posOrderUnRecur(Node head) {
        System.out.print("pos-order: ");
        if (head != null) {
            Stack<Node> s1 = new Stack<>();
            Stack<Node> s2 = new Stack<>();
            s1.push(head);
            while (!s1.isEmpty()) {
                head = s1.pop();
                s2.push(head);
                if (head.left != null)
                    s1.push(head.left);
                if (head.right != null)
                    s1.push(head.right);
            }
            while (!s2.isEmpty())
                System.out.print(s2.pop().value + " ");
        }
        System.out.println();
    }

3.3   非递归行为实现中序遍历       思路:首先将整棵树的左边界进栈,依次弹出的过程中:弹出就打印,如果弹出的节点有右节点,对它的右数也这么干,即右数的左边界进栈,然后弹出

public static void inOrderUnRecur(Node head) {
        System.out.print("in-order: ");
        if (head != null) {
            Stack<Node> stack = new Stack<>();
            while (!stack.isEmpty() || head != null) {
                if (head != null) {   //这个分支在不停的将左边界进栈
                    stack.push(head);
                    head = head.left;
                } else {            //当head延左窜到空的位置时,开始弹出并打印
                    head = stack.pop();
                    System.out.print(head.value + " ");
                    head = head.right;    //head去往它的右孩子,如果它有右孩子,那么又会进入逻辑分支1,继续处理左边界
                }
            }
        }
        System.out.println();
    }

4.二叉树的层次遍历与求二叉树的最大宽度

4.1 宽度优先遍历,也就是层次遍历       思路:用队列。 先吧头节点放入,然后弹出过程中,先放左在放右

public static void withTraversal(Node head) {
        if (head == null) return;
        Queue<Node> queue = new LinkedList<>();
        queue.add(head);
        while (!queue.isEmpty()) {
            Node cur = queue.poll();
            System.out.println(cur.value + " ");
            if (cur.left != null) queue.add(cur.left);
            if (cur.right != null) queue.add(cur.right);
        }
    }

4.2求二叉树的最大宽度            思路:我需要知道每一个节点在哪一层,将其登记到hashMap中,然后借助宽度优先遍历,从第一层开始统计,当节点跳转到下一层时,就可以得出上一层的节点数,看该数目是否大于max,更新max,然后开始统计下一层的节点

public static int w(Node head) {
        if (head == null)
            return 0;
        Queue<Node> queue = new LinkedList<>();
        queue.add(head);
        HashMap<Node, Integer> levelMap = new HashMap<>();
        levelMap.put(head, 1);
        int curLevel = 1;   //当前在统计第几层
        int curLevelNodes = 0;   //当前层的节点数量
        int max = Integer.MIN_VALUE;   //max初始为系统最小

        while (!queue.isEmpty()) {
            Node cur = queue.poll();
            int curNodeLevel = levelMap.get(cur);
            if (curNodeLevel == curLevel) {
                curLevelNodes++;    //是当前层,节点树++
            } else {
                max = Math.max(max, curLevelNodes);  //不是当前层,上一层可以进行计算了,跟新max值
                curLevel++;   //统计层数来到下一层
                curLevelNodes = 1;   //当前已经发现一个节点了,重置发现的节点数量
            }

            if (cur.left != null) {
                levelMap.put(cur.left, curNodeLevel + 1);
                queue.add(cur.left);
            }

            if (cur.right != null) {
                levelMap.put(cur.right, curNodeLevel + 1);
                queue.add(cur.right);
            }

        }
        return max;
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值