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,从下层开始继续遍历
                }
            }
        }
    }
  • 相关阅读:
    JBoss+Ant实现EJB无状态会话bean实例
    Nginx简单介绍
    SVN版本号管理工具使用中常见的代码提交冲突问题的解决方法
    深入分析Java中的I/O类的特征及适用场合
    ZOJ 3689 Digging(贪心+dp)
    uva 10641 (来当雷锋的这回....)
    Java编程中“为了性能”尽量要做到的一些地方
    wikioi 1306 机智Trie树
    PE文件结构(三) 输入表
    初始化的数值(int、double等)(一)
  • 原文地址:https://www.cnblogs.com/niutao/p/10628778.html
Copyright © 2011-2022 走看看