二叉树逐层遍历打印

有一棵二叉树,请设计一个算法,按照层次打印这棵二叉树。给定二叉树的根结点root,请返回打印结果

思路:使用一个节点队列queue和两个节点指针last和nlast,last表示上一行最后的元素.nlast表示当前正在访问的结点,初始化时nlast=last=root。不断取出队列第一个元素x,然后将x的左右孩子入队并移动nlast到最后一个孩子。然后判断x是否是last,是则打印并换行并将last指向nlast(开始下一行),否则普通打印。

Queue中用到的方法:

offer,add区别:

一些队列有大小限制,因此如果想在一个满的队列中加入一个新项,多出的项就会被拒绝。

这时新的 offer 方法就可以起作用了。它不是对调用 add() 方法抛出一个 unchecked 异常,而只是得到由 offer() 返回的 false。 

poll,remove区别:

remove() 和 poll() 方法都是从队列中删除第一个元素。remove() 的行为与 Collection 接口的版本相似,

但是新的 poll() 方法在用空集合调用时不是抛出异常,只是返回 null。因此新的方法更适合容易出现异常条件的情况。

peek,element区别:

element() 和 peek() 用于在队列的头部查询元素。与 remove() 方法类似,在队列为空时, element() 抛出一个异常,而 peek() 返回 null

代码:

import java.util.LinkedList;

public class BinaryTreeNode {
    private int value;
    private BinaryTreeNode leftChild;
    private BinaryTreeNode rightChild;

    public BinaryTreeNode() {
    }

    public BinaryTreeNode(int value, BinaryTreeNode leftChild, BinaryTreeNode rightChild) {
        this.value = value;
        this.leftChild = leftChild;
        this.rightChild = rightChild;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public BinaryTreeNode getLeftChild() {
        return leftChild;
    }

    public void setLeftChild(BinaryTreeNode leftChild) {
        this.leftChild = leftChild;
    }

    public BinaryTreeNode getRightChild() {
        return rightChild;
    }

    public void setRightChild(BinaryTreeNode rightChild) {
        this.rightChild = rightChild;
    }

    public void print(){
        LinkedList<BinaryTreeNode> queue=new LinkedList<>();
        queue.add(this);
        BinaryTreeNode last = this;
        BinaryTreeNode nlast = this;
        while(!queue.isEmpty()){
            BinaryTreeNode node = queue.poll();
            if(node.getLeftChild() != null){
                queue.add(node.getLeftChild());
                nlast = queue.getLast();
            }
            if(node.getRightChild() != null){
                queue.add(node.getRightChild());
                nlast = queue.getLast();
            }

            if(last == node){

                System.out.print(node.getValue()+"\n");
                last = nlast;

            }else {
                System.out.print(node.getValue()+" ");

            }
        }
    }
    public static void main(String[] args){
        BinaryTreeNode root=new BinaryTreeNode(1, null, null);
        BinaryTreeNode node2=new BinaryTreeNode(2, null, null);
        BinaryTreeNode node3=new BinaryTreeNode(3, null, null);
        BinaryTreeNode node4=new BinaryTreeNode(4, null, null);
        BinaryTreeNode node5=new BinaryTreeNode(5, null, null);
        BinaryTreeNode node6=new BinaryTreeNode(6, null, null);
        BinaryTreeNode node7=new BinaryTreeNode(7, null, null);
        BinaryTreeNode node8=new BinaryTreeNode(8, null, null);
        BinaryTreeNode node9=new BinaryTreeNode(9, null, null);
        BinaryTreeNode node10=new BinaryTreeNode(10, null, null);


        root.setLeftChild(node2);
        root.setRightChild(node3);
        node2.setLeftChild(node4);
        node3.setLeftChild(node5);
        node3.setRightChild(node6);
        node4.setRightChild(node7);
        node5.setLeftChild(node8);
        node5.setRightChild(node9);
        node8.setRightChild(node10);

        root.print();
    }

}

结果:

1
2 3
4 5 6
7 8 9
10

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值