剑指-从上往下打印二叉树

题目:
从上往下打印二叉树的每个结点,同一层的结点按照从左到右的顺序打印.

思路:

方法1:
每一次打印一个结点的时候,如果该结点有子结点,则把该结点的子节点放到一个队列的末尾. 接下再来到队列的头部取出最早进入队列的结点,重复前面的打印操作,直至队列中所有的结点都被打印出来为止.

方法2:
通过递归方法

private static class Node {
    int value;
    Node left;
    Node right;

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

public static void main(String[] args) {
    Node root = new Node(8);

    Node _6 = new Node(6);
    Node _10 = new Node(10);
    root.left = _6;
    root.right = _10;

    Node _5 = new Node(5);
    Node _7 = new Node(7);
    _6.left = _5;
    _6.right = _7;

    Node _9 = new Node(9);
    Node _11 = new Node(11);
    _10.left = _9;
    _10.right = _11;

    print(root);

    printNodeByLevel(root);
}

static void print(Node root) {
    LinkedList<Node> queue = new LinkedList<>();
    queue.offer(root);
    while (!queue.isEmpty()) {

        Node node = queue.poll();

        System.out.printf(node.value + " ");

        if (node.left != null) {
            queue.offer(node.left);
        }
        if (node.right != null) {
            queue.offer(node.right);
        }
    }
}

private static int printNodeAtLevel(Node node, int level) {
    if (node == null || level < 0) {
        return 0;
    }
    if (level == 0) {
        System.out.printf(node.value + " ");
        return 1;
    }

    return printNodeAtLevel(node.left, level - 1) + printNodeAtLevel(node.right, level - 1);
}

private static void printNodeByLevel(Node node) {
    if (node == null) {
        return;
    }

    for (int level = 0; ; level++) {
        if (printNodeAtLevel(node, level) == 0) {
            break;
        }
        System.out.println();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值