zoukankan      html  css  js  c++  java
  • 面试题:分层打印二叉树

    需求:

    树的结构

    //                1
    //           /        
    //         2           3
    //       /           /  
    //      4     5     6    7

    需要按照如下的方式进行打印:

    1
    23
    4567

    分析:

    本文按照非递归的方式进行打印,其中的关键就是:

    1):使用队列保存当前要打印的节点

    2):当前层打印完毕后换行

    代码:

     public static void printTree(BineryTreeNode root){
            //首先做一个安全的判断
            if(root == null){
                return ;
            }
            //用一个队列存储当前要打印的节点
            Queue<BineryTreeNode> queue = new LinkedList<BineryTreeNode>();
            int current; //当前层还未打印的节点
            int next ; //下一层的节点个数
    
            queue.add(root);//开始将头节点放入队列
            current = 1 ;
            next = 0 ;
            while (!queue.isEmpty()){//如果当前的队列不为空
                final BineryTreeNode currentNode = queue.poll();//弹出当前队列的值
                System.out.print(currentNode.data + "  ");
                current -- ;
    
                //打印左子树
                if(currentNode.left != null){
                    queue.add(currentNode.left);
                    next ++ ;
                }
    
                //打印右子树
                if(currentNode.right != null){
                    queue.add(currentNode.right);
                    next ++ ;
                }
                if(current == 0){//如果current为0,说明当前层打印完毕
                    System.out.println();//换行
                    current = next ;//下一节点没打印的个数就是:左子树 + 右子树
                    next = 0 ;//将next至0,从下层开始继续遍历
                }
            }
        }

    全部代码:

    package demo2;
    
    import jdk.nashorn.internal.ir.BinaryNode;
    
    import java.util.LinkedList;
    import java.util.Queue;
    
    /**
     * Created by angel
     */
    
    class BineryTreeNode{
        public int data ;
        public BineryTreeNode left ; //树的左节点
        public BineryTreeNode right ; //树的右节点
        public BineryTreeNode(int data){
            this.data = data ;
        }
    
        @Override
        public String toString() {
            return "[data" + data + " , left = " + left + " , right = "+ right +"]" ;
        }
    }
    
    public class PrintBineryTree {
    
        public static void main(String[] args) {
            BineryTreeNode node1 = new BineryTreeNode(1);
            BineryTreeNode node2 = new BineryTreeNode(2);
            BineryTreeNode node3 = new BineryTreeNode(3);
            BineryTreeNode node4 = new BineryTreeNode(4);
            BineryTreeNode node5 = new BineryTreeNode(5);
            BineryTreeNode node6 = new BineryTreeNode(6);
            BineryTreeNode node7 = new BineryTreeNode(7);
            node1.left = node2 ;
            node1.right= node3 ;
            node2.left = node4 ;
            node2.right = node5 ;
            node3.left = node6 ;
            node3.right = node7 ;
    
            printTree(node1);
        }
    
        public static void printTree(BineryTreeNode root){
            //首先做一个安全的判断
            if(root == null){
                return ;
            }
            //用一个队列存储当前要打印的节点
            Queue<BineryTreeNode> queue = new LinkedList<BineryTreeNode>();
            int current; //当前层还未打印的节点
            int next ; //下一层的节点个数
    
            queue.add(root);//开始将头节点放入队列
            current = 1 ;
            next = 0 ;
            while (!queue.isEmpty()){//如果当前的队列不为空
                final BineryTreeNode currentNode = queue.poll();//弹出当前队列的值
                System.out.print(currentNode.data + "  ");
                current -- ;
    
                //打印左子树
                if(currentNode.left != null){
                    queue.add(currentNode.left);
                    next ++ ;
                }
    
                //打印右子树
                if(currentNode.right != null){
                    queue.add(currentNode.right);
                    next ++ ;
                }
                if(current == 0){//如果current为0,说明当前层打印完毕
                    System.out.println();//换行
                    current = next ;//下一节点没打印的个数就是:左子树 + 右子树
                    next = 0 ;//将next至0,从下层开始继续遍历
                }
            }
        }
    }
  • 相关阅读:
    Error: unable to load xmlsec-openssl library
    count(1)、count(*)与count(列名)的执行区别
    Linux下的压缩zip,解压缩unzip命令详解及实例
    linux centos 如何查看操作系统版本信息
    These dependencies were not found: *!!vue-style-loader!css-loader?
    Git如何永久删除某个重要文件文件或文件夹 (包括历史记录) 强制
    LDAP的filter查询详解
    详谈mysqldump数据导出的问题
    GO -- 遍历删除 数组 slice
    mjml强大&&灵活的邮件模版引擎
  • 原文地址:https://www.cnblogs.com/niutao/p/10628778.html
Copyright © 2011-2022 走看看