从上到下打印二叉树(Java实现)

问题描述

从上到下打印如下结构的二叉树:

public class E32PrintBinaryTreeFromTop {
    //从上到下打印二叉树  
    private static class BinaryTreeNode{
    	int value;
    	BinaryTreeNode left;
    	BinaryTreeNode right;
    }
}

问题一: 不分行从上到下打印二叉树

public static void printFromTopToButton(BinaryTreeNode root){
    //不分行打印二叉树
    if (root == null)
        return;

    Queue<BinaryTreeNode> nodes = new LinkedList<>();
    nodes.offer(root);
    while(!nodes.isEmpty()){
        BinaryTreeNode node = nodes.poll();
        System.out.printf("%d\t", node.value);
        if (node.left != null)
            nodes.offer(node.left);
        if (node.right != null)
            nodes.offer(node.right);
    }
}

问题二: 分行从上到下打印二叉树

public static void printLineByLine(BinaryTreeNode root){
    //分行打印二叉树
    if(root == null)
        return;

    Queue<BinaryTreeNode> nodes = new LinkedList<>();
    nodes.offer(root);
    //这一行及下一行需要打印的节点数量
    int currentLine = 1;
    int nextLine = 0;
    while(!nodes.isEmpty()){
        BinaryTreeNode node = nodes.poll();
        currentLine --;
        System.out.printf("%d\t", node.value);
        if (node.left != null){
            nodes.offer(node.left);
            nextLine ++;
        }
        if (node.right != null){
            nodes.offer(node.right);
            nextLine ++;
        }
        //换行
        if (currentLine == 0){
            System.out.print("\n");
            currentLine = nextLine;
            nextLine = 0;
        }
    }
}

问题三: 之字形打印二叉树

public static void printZigzagScan(BinaryTreeNode root){
    //按之字形分行打印二叉树
    if (root == null)
        return;
    //Java中不允许使用泛型数组,只能使用?或者强制转换
    //使用一个栈当前行剩余元素的出栈会被下一行元素的入栈阻挡
    Stack<BinaryTreeNode>[] orders = new Stack[2];
    orders[0] = new Stack<>();
    orders[1] = new Stack<>();
    //设置两个状态位来表示当前行与下一行的打印顺序
    int currentOrder = 0;
    int nextOrder = 1;
    orders[0].push(root);
    while(!orders[0].isEmpty() || !orders[1].isEmpty()){
        BinaryTreeNode node = orders[currentOrder].pop();
        System.out.printf("%d\t", node.value);
        if (currentOrder == 0){
            if (node.left != null)
                orders[nextOrder].push(node.left);
            if (node.right != null)
                orders[nextOrder].push(node.right);
        }
        else{
            if (node.right != null)
                orders[nextOrder].push(node.right);
            if (node.left != null)
                orders[nextOrder].push(node.left);
        }

        if (orders[currentOrder].isEmpty()){
            System.out.print("\n");
            currentOrder = 1 - currentOrder;
            nextOrder = 1 - nextOrder;
        }
    }
}

测试用例

//根据从上到下从左至右的二叉树遍历序列重构二叉树,用于测试
private static BinaryTreeNode construct(int[] order, int index){
    if (index >= order.length)
        return null;
    BinaryTreeNode node = new BinaryTreeNode();
    node.value = order[index];
    node.left = construct(order, index * 2 + 1);
    node.right = construct(order, index * 2 + 2);
    return node;
}

//测试用例
public static void main(String[] args){
    int[] order = {1, 2, 3, 4, 5, 6, 7};
    BinaryTreeNode root = construct(order, 0);
    E32PrintBinaryTreeFromTop.printFromTopToButton(root);
    System.out.print("\n");
    E32PrintBinaryTreeFromTop.printLineByLine(root);
    System.out.print("\n");
    E32PrintBinaryTreeFromTop.printZigzagScan(root);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值