二叉树的三种层次遍历

package class07;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
/*
* flag:发现一层结束
*简答实现;单纯打印
* 每层打印:发现每层的开始和结束
*
* 实现一:Map:poll后比较curLevel,!=就poll的是新层;否则还是该层
* Map<Node,Integer>--<node,deep>:每个结点记录了宽度
* int curLevel(deep)=1:正在统计哪一层
* int curLevelNodes=0:当前层的宽度
* int max:最大宽度
* 实现二:加变量
* */
public class LevelTraversalBT {
    public static class Node{
        Node left;
        Node right;
        int value;
        public Node(int value) {
            this.value = value;
        }
    }
    //简单实现
    public static void Traversal(Node head){
        if (head==null) return;
        Queue<Node> queue=new LinkedList<>();
        queue.add(head);
        while(!queue.isEmpty()){
            Node node = queue.poll();
            System.out.println(node.value);
            if(node.left!=null)  queue.add(node.left);
            if(node.right!=null) queue.add(node.right);
        }
    }



    //发现层的结束:求最大宽度:再入队的时候peek,判断更新层数的时候打印会按层打印
    /*
    * 出队获取curNodeLevel;孩子在入队一:
    *
    * 判断curNodeLevel和curLevel是否一样,not 则更新curLevel
    * 注意点:孩子入队,Map的值是curNodeLevel+1;
    *       最后要更新一次max否则max不能比较最后层
    * */
    public static int TraversalByMap(Node head) {
        if (head == null) return 0;
        Queue<Node> queue = new LinkedList<>();
        queue.offer(head);

        HashMap<Node, Integer> map = new HashMap<>();
        map.put(head, 1);
        int curLevel = 1;//正在统计的层
        int curLevelNodes = 0;//正在统计层的个数
        int max = 0;//最大宽度

        while (!queue.isEmpty()) {
            Node node = queue.peek();
            int curNodeLevel = map.get(node);//当前结点的层数
            //入队记录结点层
            if (node.left != null) {
                queue.offer(node.left);
                map.put(node.left, curNodeLevel + 1);//curLevel!=curNodeLevel
            }
            if (node.right != null) {
                queue.offer(node.right);
                map.put(node.right, curNodeLevel + 1);
            }
            //弹出累加结点:判断是否进入下一层
            if (curNodeLevel == curLevel){ curLevelNodes++;
                System.out.print(queue.poll().value+"  ");}
            else {
                //进入新层更新变量
                max = Math.max(max, curLevelNodes);
                curLevel++;
                curLevelNodes=1;
                System.out.println();
                System.out.print(queue.poll().value+"  ");
            }
        }
        max=Math.max(max,curLevelNodes);//最后一层只会更新累计结点数,不会更新curLevel;
        return max;
    }


    //发现新层:curEnd:当前层最右结点;nextEnd:下一层的最右边结点
    //只要发现了cur弹到了curEnd-->马上进入新层;curEnd=nextEnd--->nextEnd再去寻找它的最右边
    public static int TraversalByCurEndAndNextEnd(Node head){
        if(head==null) return 0;
        Queue<Node> queue=new LinkedList<>();
        queue.offer(head);
        int max=0;
        int curLevelNodes=0;
        Node curEnd=head;
        Node nextEnd=null;
        while(!queue.isEmpty()){
            Node cur = queue.poll();
            if(cur.left!=null) {
                queue.offer(cur.left);
                nextEnd=cur.left;
            }
            if(cur.right!=null){
                queue.offer(cur.right);
                nextEnd=cur.right;
            }
            curLevelNodes++;
            System.out.print(cur.value+"  ");
            if(cur==curEnd){
                curEnd=nextEnd;
                max=Math.max(max,curLevelNodes);
                curLevelNodes=0;
                System.out.println();
            }
        }
        return max;

    }



    public static void main(String[] args) {
        Node node0=new Node(1);
        Node node1=new Node(2);
        Node node2=new Node(3);
        Node node3=new Node(1);
        node0.left=node1;
        node0.right=node2;
        node2.left=node3;
//        System.out.println("                  "+TraversalByMap(node0));
//        Traversal(node0);
        System.out.println("           "+TraversalByCurEndAndNextEnd(node0));
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赑屃爱Java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值