public class HelloWorld{
public static void main(String []args){
print(6);
}
public static void print(int n){
int size = ((n+1) * n)/2;
int[] data = new int[size];
data[0] = 1;
int current = 0;
for (int level = 0; level < n; level++){
int nextLevel = level + 1;
for (int space = n; space > nextLevel; space--){
System.out.print(" ");
}
for (int count = 0; count < nextLevel; count++){
//print current
System.out.print(data[current]+" ");
//process only non leaf node
if (nextLevel < n){
//add to child where left child = current + nextLevel
data[current + nextLevel] += data[current];
//right child = current + nextLevel + 1
data[current + nextLevel + 1] += data[current];
}
current++;
}
System.out.println();
}
}
}
public static void main(String []args){
print(6);
}
public static void print(int n){
int size = ((n+1) * n)/2;
int[] data = new int[size];
data[0] = 1;
int current = 0;
for (int level = 0; level < n; level++){
int nextLevel = level + 1;
for (int space = n; space > nextLevel; space--){
System.out.print(" ");
}
for (int count = 0; count < nextLevel; count++){
//print current
System.out.print(data[current]+" ");
//process only non leaf node
if (nextLevel < n){
//add to child where left child = current + nextLevel
data[current + nextLevel] += data[current];
//right child = current + nextLevel + 1
data[current + nextLevel + 1] += data[current];
}
current++;
}
System.out.println();
}
}
}
No comments:
Post a Comment