分行从上到下打印二叉树

题目:分行从上到下打印二叉树。从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。
这道题和前面的题类似,也可以用一个队列来保存将要打印的节点。
为了把二叉树的每一行单独打印到一行里,
我们需要两个变量;
一个变量表示在当前层中还没有打印的节点数;
另一个变量表示下一层节点的数目。
函数的代码如下

  public void printTree(TreeNode root){
        if(root==null){
            return;
        }
        LinkedList<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        TreeNode node=null;
        int countNext=1;
        int pCount=0;

        while(!queue.isEmpty()){
            pCount=countNext;
            countNext=0;
            for(int i=0;i<pCount;i++){
                node=queue.poll();
                System.out.print(node.val+" ");
                if(node.left!=null){
                    queue.offer(node.left);
                    countNext++;
                }
                if(node.right!=null){
                    queue.offer(node.right);
                    countNext++;
                }
            }
            System.out.println();


        }
        System.out.println();


    }

完整代码带测试

package com.helan.d;

import java.util.LinkedList;

public class PirntfTreeNode {
    public static class TreeNode{
        int val;
        TreeNode right;
        TreeNode left;
        public TreeNode(){
        }
        public TreeNode(int val){
            this.val=val;
        }
    }
    public void printTree(TreeNode root){
        if(root==null){
            return;
        }
        LinkedList<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        TreeNode node=null;
        int countNext=1;
        int pCount=0;

        while(!queue.isEmpty()){
            pCount=countNext;
            countNext=0;
            for(int i=0;i<pCount;i++){
                node=queue.poll();
                System.out.print(node.val+" ");
                if(node.left!=null){
                    queue.offer(node.left);
                    countNext++;
                }
                if(node.right!=null){
                    queue.offer(node.right);
                    countNext++;
                }
            }
            System.out.println();


        }
        System.out.println();


    }
    public TreeNode createBinaryTreeNode(int value){
        TreeNode pNode=new TreeNode();
        pNode.val=value;
        pNode.left=null;
        pNode.right=null;
        return pNode;
    }
    public void connectTreeNodes(TreeNode pParent,TreeNode pLeft,TreeNode pRight){
        if(pParent!=null){
            pParent.left=pLeft;
            pParent.right=pRight;
        }
    }
    public void print(TreeNode pRoot){
        printTreeNode(pRoot);
    }

    private void printTreeNode(TreeNode pNode) {
        if(pNode!=null){
            System.out.printf("节点的值是:%d\n",pNode.val);

            if(pNode.left!=null){
                System.out.printf("节点的左子节点的值是值是:%d\n",pNode.left.val);
            }else{
                System.out.printf("节点的左子节点为空\n");
            }
            if(pNode.right!=null){
                System.out.printf("节点的右子节点的值是值是:%d\n",pNode.right.val);
            }else{
                System.out.printf("节点的右子节点为空\n");
            }

        }else{
            System.out.println("节点为空\n");
        }
        System.out.println();
    }
    // ====================测试代码====================
    void test(String testName, TreeNode pRoot){
        if(testName != null)
            System.out.printf("%s begins: \n", testName);

        printTree(pRoot);

        System.out.printf("The nodes from top to bottom, from left to right are: \n");
        print(pRoot);

        System.out.printf("\n\n");
    }

    //         10
//         /      \
//        6        14
//       /\        /\
//      4  8     12  16
    void test1() {
        TreeNode pNode10 = createBinaryTreeNode(10);
        TreeNode pNode6 = createBinaryTreeNode(6);
        TreeNode pNode14 = createBinaryTreeNode(14);
        TreeNode pNode4 = createBinaryTreeNode(4);
        TreeNode pNode8 = createBinaryTreeNode(8);
        TreeNode pNode12 = createBinaryTreeNode(12);
        TreeNode pNode16 = createBinaryTreeNode(16);

        connectTreeNodes(pNode10, pNode6, pNode14);
        connectTreeNodes(pNode6, pNode4, pNode8);
        connectTreeNodes(pNode14, pNode12, pNode16);

        test("Test1", pNode10);


    }

    //             5
//              /
//             4
//            /
//           3
//          /
//         2
//        /
//       1
    void test2()
    {
        TreeNode pNode5 = createBinaryTreeNode(5);
        TreeNode pNode4 = createBinaryTreeNode(4);
        TreeNode pNode3 = createBinaryTreeNode(3);
        TreeNode pNode2 = createBinaryTreeNode(2);
        TreeNode pNode1 = createBinaryTreeNode(1);

        connectTreeNodes(pNode5, pNode4, null);
        connectTreeNodes(pNode4, pNode3, null);
        connectTreeNodes(pNode3, pNode2, null);
        connectTreeNodes(pNode2, pNode1, null);

        test("Test2", pNode5);


    }

    // 1
//  \
//   2
//    \
//     3
//      \
//       4
//        \
//         5
    void test3()
    {
        TreeNode pNode1 = createBinaryTreeNode(1);
        TreeNode pNode2 = createBinaryTreeNode(2);
        TreeNode pNode3 = createBinaryTreeNode(3);
        TreeNode pNode4 = createBinaryTreeNode(4);
        TreeNode pNode5 = createBinaryTreeNode(5);

        connectTreeNodes(pNode1, null, pNode2);
        connectTreeNodes(pNode2, null, pNode3);
        connectTreeNodes(pNode3, null, pNode4);
        connectTreeNodes(pNode4, null, pNode5);

        test("Test3", pNode1);


    }

    // 树中只有1个结点
    void test4(){
        TreeNode pNode1 = createBinaryTreeNode(1);
        test("Test4", pNode1);


    }

    // 树中没有结点
    void test5(){
        test("Test5", null);
    }

    public static void main(String[] args) {
        new PirntfTreeNode().test1();
        new PirntfTreeNode().test2();
        new PirntfTreeNode().test3();
        new PirntfTreeNode().test4();
        new PirntfTreeNode().test5();

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值