java 从上至下打印二叉树

从上往下打印二叉树
题目描述:

从上往下打印出二叉树的每个节点,同层节点从左至右打印。
输入:
输入可能包含多个测试样例。
对于每个测试案例,输入的第一行一个整数n(1<=n<=1000, :n代表将要输入的二叉树元素的个数(节点从1开始编号)。接下来一行有n个数字,代表第i个二叉树节点的元素的值。接下来有n行,每行有一个字母Ci。
Ci=’d’表示第i个节点有两子孩子,紧接着是左孩子编号和右孩子编号。
Ci=’l’表示第i个节点有一个左孩子,紧接着是左孩子的编号。
Ci=’r’表示第i个节点有一个右孩子,紧接着是右孩子的编号。
Ci=’z’表示第i个节点没有子孩子。
输出:
对应每个测试案例,
按照从上之下,从左至右打印出二叉树节点的值。
样例输入:
7
8 6 5 7 10 9 11
d 2 5
d 3 4
z
z
d 6 7
z
z
样例输出:
8 6 10 5 7 9 11
 
二叉树的实现记得一般都是用指针,左儿子,右儿子,什么的。
但是自己觉得可以用数组来实现一发。、
下面就是用数组实现的代码,主要是存储二叉树,和一层一层的打印。
 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Scanner in = new Scanner(System.in) ;
 8         int node[][] = new int [1005][4] ; ///0为节点的值,1左孩子,2父节点,3右孩子
 9         while(in.hasNextInt()){
10             int n = in.nextInt() ;
11             for(int i=1;i<=n;i++){
12                 node[i][0] = in.nextInt() ;
13             }
14             for(int i=1;i<=n;i++){ ///节点的初始化
15                 node[i][2] = i ;
16                 node[i][1]=node[i][3]=0 ;
17             }
18             for(int i=1;i<=n;i++){
19                 String str = in.next() ;
20                 if(str.equals("d")){
21                     int x = in.nextInt() ;
22                     int y = in.nextInt() ;
23                     node[i][1] = x ;
24                     node[i][3] = y ;
25                     node[x][2] = i ;
26                     node[y][2] = i ;
27                 }else if(str.equals("l")){
28                     int x = in.nextInt() ;
29                     node[i][1] = x ;
30                     node[i][3] = 0 ;
31                     node[x][2] = i ;
32                 }else if(str.equals("r")){
33                     int x = in.nextInt() ;
34                     node[i][1] = 0 ;
35                     node[i][3] = x ;
36                     node[x][2] = i ;
37                 }
38             }
39             int root = 1 ;  ///寻找根节点
40             while(root != node[root][2] ){
41                 root = node[root][2] ;
42             }
43             //System.out.println(root);
44             int arry1[] = new int[1005];
45             int arry2[] = new int[1005];
46             arry1[1] = root ;
47             int cnt = 2 ;
48             //System.out.println("yes");
49             
50             ///开始打印二叉树
51             System.out.print(node[root][0]);
52             while(true){
53                 if(arry1[1]!=root){ ///不再打印根节点
54                     for(int i=1;i<cnt;i++){
55                         System.out.print(" "+node[arry1[i]][0]);
56                     }
57                 }
58                 int c = 1 ;
59                 for(int i=1;i<cnt;i++){ 
60                     if(node[arry1[i]][1]!=0){  ///如果有左孩子
61                         arry2[c]=node[arry1[i]][1] ;
62                         c++ ;
63                     }
64                     if(node[arry1[i]][3]!=0){  ///如果有右孩子
65                         arry2[c]=node[arry1[i]][3] ;
66                         c++ ;
67                     }
68                 }
69                 cnt=c ;
70                 for(int i=1;i<cnt;i++){
71                     arry1[i] = arry2[i] ; 
72                 }
73                 if(c==1)break ;
74             }
75         }
76     }
77 }

 

posted @ 2016-03-04 21:18  方凌飞2014551539  阅读(2597)  评论(0编辑  收藏  举报