美文网首页
【剑指Offer 23】从上到下打印二叉树

【剑指Offer 23】从上到下打印二叉树

作者: 3e1094b2ef7b | 来源:发表于2017-07-17 16:15 被阅读7次

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

    代码如下:

    package demo;
    
    import java.util.LinkedList;
    import java.util.Queue;
    
    public class Test23 {
        public static class BinaryTreeNode {
            int value;
            BinaryTreeNode left;
            BinaryTreeNode right;
        }
    
        public static void printFromTopToBottom(BinaryTreeNode root) {
            if (root != null) {
                // 存放还未遍历的元素
                Queue<BinaryTreeNode> list = new LinkedList<>();
                // 将根节点入队
                list.add(root);
                // 记录当前处理的结点
                BinaryTreeNode curNode;
                // 如果队列非空,则一直进行处理
                while (!list.isEmpty()) {
                    // 删除对首元素
                    curNode = list.remove();
                    // 输出对首元素的值
                    System.out.print(curNode.value + " ");
                    // 如果左子结点不为空,则左子结点入队
                    if (curNode.left != null) {
                        list.add(curNode.left);
                    }
                    // 如果右子结点不为空,则右子结点入队
                    if (curNode.right != null) {
                        list.add(curNode.right);
                    }
                }
            }
        }
    }
    

    测试1:

    测试1
    public static void main(String[] args) {
        BinaryTreeNode root = new BinaryTreeNode();
        root.value = 8;
        root.left = new BinaryTreeNode();
        root.left.value = 6;
        root.right = new BinaryTreeNode();
        root.right.value = 10;
        root.left.left = new BinaryTreeNode();
        root.left.left.value = 5;
        root.left.right = new BinaryTreeNode();
        root.left.right.value = 7;
        root.right.left = new BinaryTreeNode();
        root.right.left.value = 9;
        root.right.right = new BinaryTreeNode();
        root.right.right.value = 11;
        printFromTopToBottom(root);
    }
    
    运行结果

    测试2:

    测试2
    public static void main(String[] args) {
        BinaryTreeNode root2 = new BinaryTreeNode();
        root2.value = 1;
        root2.left = new BinaryTreeNode();
        root2.left.value = 3;
        root2.left.left = new BinaryTreeNode();
        root2.left.left.value = 5;
        root2.left.left.left = new BinaryTreeNode();
        root2.left.left.left.value = 7;
        root2.left.left.left.left = new BinaryTreeNode();
        root2.left.left.left.left.value = 9;
        printFromTopToBottom(root2);
    }
    
    运行结果

    测试3:

    测试3
    public static void main(String[] args) {
        BinaryTreeNode root3 = new BinaryTreeNode();
        root3.value = 0;
        root3.right = new BinaryTreeNode();
        root3.right.value = 2;
        root3.right.right = new BinaryTreeNode();
        root3.right.right.value = 4;
        root3.right.right.right = new BinaryTreeNode();
        root3.right.right.right.value = 6;
        root3.right.right.right.right = new BinaryTreeNode();
        root3.right.right.right.right.value = 8;
        printFromTopToBottom(root3);
    }
    
    运行结果

    测试4:

    public static void main(String[] args) {
        BinaryTreeNode root3 = new BinaryTreeNode();
        root3.value = 1;
        printFromTopToBottom(root3);
    }
    
    运行结果

    测试5:

    public static void main(String[] args) {
        printFromTopToBottom(null);
    }
    

    来源:http://blog.csdn.net/derrantcm/article/details/46705719

    相关文章

      网友评论

          本文标题:【剑指Offer 23】从上到下打印二叉树

          本文链接:https://www.haomeiwen.com/subject/nrfqkxtx.html