zoukankan      html  css  js  c++  java
  • 剑指offer系列20--从上到下打印二叉树

    * 20 [题目]从上往下打印出二叉树的每个节点,同层节点从左至右打印。
    * 【思路】从根结点开始,先保存结点,再看根结点的左右结点有没有值。
    * 有,就将左右值放到集合中;
    * 根节点输出后,打印根结点左结点并将根结点左结点的左右结点保存;打印根结点右结点并将根结点右结点的左右结点保存。。

     1 package com.exe4.offer;
     2 
     3 import java.util.ArrayList;
     4 
     5 /**
     6  * 20 [题目]从上往下打印出二叉树的每个节点,同层节点从左至右打印。
     7  *    【思路】从根结点开始,先保存结点,再看根结点的左右结点有没有值。
     8  *           有,就将左右值放到集合中;
     9  *          根节点输出后,打印根结点左结点并将根结点左结点的左右结点保存;打印根结点右结点并将根结点右结点的左右结点保存。。
    10  *            
    11  * @author WGS
    12  *
    13  */
    14 public class PrintBitTree {
    15     
    16     public static class TreeNode{
    17         int val=0;
    18         TreeNode left=null;
    19         TreeNode right=null;
    20         public TreeNode(int n){
    21             this.val=n;
    22         }
    23     }
    24 
    25     public  void printBitTreeFromTopToBottom(TreeNode rootNode){
    26         
    27         if(rootNode==null) return;
    28         ArrayList<TreeNode> list=new ArrayList<>();//保存遍历的节点
    29         ArrayList<Integer> data=new ArrayList<>();//保存最后输出的遍历节点
    30         int index=1;
    31         
    32         list.add(rootNode);
    33         
    34         while(list.size()>0){
    35             //移除后,下一位即0位。比如8在0位,移除后6即在0位。
    36             //此时6由1位》0位,10由2位变为1位。
    37             //所以下次存储时要在2位存储,不是在3位,所以此处index--
    38             TreeNode temp=list.remove(0);//8        
    39             index--;
    40             //data.add(temp.val);//8  6   10...
    41             System.out.print(temp.val+" ");
    42             if(temp.left!=null){
    43                 list.add(index++, temp.left);//1位6   
    44             }
    45             if(temp.right!=null){
    46                 list.add(index++, temp.right);//2位10
    47             }
    48         }
    49 
    50     }
    51     public static void main(String[] args) {
    52          TreeNode root = new TreeNode(8);
    53             TreeNode node1 = new TreeNode(6);
    54             TreeNode node2 = new TreeNode(10);
    55             TreeNode node3 = new TreeNode(5);
    56             TreeNode node4 = new TreeNode(7);
    57             TreeNode node5 = new TreeNode(9);
    58             TreeNode node6 = new TreeNode(11);
    59             
    60             root.left = node1;
    61             root.right = node2;
    62             node1.left = node3;
    63             node1.right = node4;
    64             node2.left = node5;
    65             node2.right = node6;
    66             
    67             new PrintBitTree().printBitTreeFromTopToBottom(root);
    68             System.out.println();
    69 /*            for (Integer integer : list) {
    70                 System.out.print(integer + " ");*/
    71 
    72 
    73     }
    74 }
  • 相关阅读:
    88、使用tensorboard进行可视化学习,查看具体使用时间,训练轮数,使用内存大小
    88、展示Tensorflow计算图上每个节点的基本信息以及运行时消耗的时间和空间
    关于实时监听输入框的值变化
    再谈javascript函数节流
    HTML5离线缓存Manifest
    javascript判断浏览器支持CSS3属性
    关于移动web开发过程中的”点透“问题
    跨域解决方案之HTML5 postMessage
    最精简的金额格式化
    Grunt usemin前端自动化打包流程
  • 原文地址:https://www.cnblogs.com/noaman/p/5454809.html
Copyright © 2011-2022 走看看