【剑指offer】面试题32(2):分行从上到下打印二叉树

完整代码地址

完整代码地址

题目

从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。

思路

用队列来保存要打印的节点。
同时我们需要两个变量:一个变量表示在当前层中还没有打印的节点数;另一个变量表示下一层节点的数目。

代码

public class _32_02_PrintTreesInLines {

    public static class TreeNode {
        public int val = 0;
        public TreeNode left = null;
        public TreeNode right = null;
        public TreeNode(int val) {
            this.val = val;
        }
    }

    public static ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
        if(pRoot == null)
            return new ArrayList<>();

        ArrayList<ArrayList<Integer>> result = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(pRoot);

        ArrayList<Integer> lineResult = new ArrayList<>();
        int numOfThisLine = 1;
        int numOfNextLine = 0;

        while(!queue.isEmpty()) {
            TreeNode node = queue.poll();
            lineResult.add(node.val);
            --numOfThisLine;
            if(node.left != null) {
                queue.add(node.left);
                ++numOfNextLine;
            }
            if(node.right != null) {
                queue.add(node.right);
                ++numOfNextLine;
            }
            if(numOfThisLine == 0) {
                result.add(lineResult);
                lineResult = new ArrayList<>();
                numOfThisLine = numOfNextLine;
                numOfNextLine = 0;
            }
        }
        return result;
    }
}

测试

public class _32_02_Test {

    public static void main(String[] args) {
        test1();
        test2();
        test3();
    }

    /**
     * 功能测试
     *     1
     *    / \
     *   2   3
     *  / \   \
     * 4   5   6
     */
    private static void test1() {
        TreeNode root = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        TreeNode node6 = new TreeNode(6);
        root.left = node2;
        root.right = node3;
        node2.left = node4;
        node2.right = node5;
        node3.right = node6;
        System.out.println(_32_02_PrintTreesInLines.Print(root));
    }

    /**
     * 边界测试
     * 1.只有一个节点
     * 2.每个节点都只有左子节点
     * 3.每个节点都只有右子节点
     */
    private static void test2() {
        TreeNode root = new TreeNode(1);

        System.out.println(_32_02_PrintTreesInLines.Print(root));

        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        root.left = node2;
        node2.left = node3;

        System.out.println(_32_02_PrintTreesInLines.Print(root));

        root.left = null;
        node2.left = null;
        root.right = node2;
        node2.right = node3;
        System.out.println(_32_02_PrintTreesInLines.Print(root));
    }

    /**
     * 极端测试 
     * null
     */
    private static void test3() {
        System.out.println(_32_02_PrintTreesInLines.Print(null));
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值