UVa699-The Falling Leaves(下落的叶子)
来源:互联网 发布:淘宝运营助理是干什么 编辑:程序博客网 时间:2024/04/28 04:56
UVa 699 - The Falling Leaves(下落的叶子)
给一棵二叉树,每个节点都有一个水平位置:左子节点在它左边1个单位,右子节点在右边一个单位。从左向右输出每个水平位置的所有节点的权值之和。按照递归(先序)方式输入,用-1表示空树。
样例输入:
5 7 -1 6 -1 -1 3 -1 -1
8 2 9 -1 -1 6 5 -1 -1 12 -1 -1 3 7 -1 -1 -1
-1
样例输出
case1:7 11 3
case2:9 7 21 15
import java.util.Scanner;public class Test{static int[] sum = new int[1000]; //全局变量public static void main(String[] args) {Scanner cin = new Scanner(System.in);int p = 0;while (root()){while (sum[p]==0)p++; //找最左边的叶子System.out.print("case: ");while (sum[p]!=0)System.out.print(sum[p++]+" "); //输出System.out.println();}}// main//边输入边统计private static boolean root(){Scanner cin = new Scanner(System.in);int v = cin.nextInt();if (v==-1) return false;//初始化数组for (int i = 0;i<sum.length;i++)sum[i]=0;//该树的水平位置,500作为根的初始位置int pos = sum.length>>1;sum[pos]=v;build(pos-1);build(pos+1);return true;}//root//输入并统计一棵子树,树根的水平位置为pprivate static void build(int p){Scanner cin = new Scanner(System.in);int v = cin.nextInt();if(v==-1) return; //空树sum[p]+=v;build(p-1);build(p+1);}//build}
0 0
- UVa699-The Falling Leaves(下落的叶子)
- UVA699 - The Falling Leaves
- UVa699 The Falling Leaves
- The Falling Leaves UVA699
- uva699 - The Falling Leaves
- uva699 the falling leaves
- UVA699 - The Falling Leaves
- UVa699 The Falling Leaves
- UVa699 The Falling Leaves
- Uva699 The Falling Leaves
- UVA699-例题6-10 下落的树叶(The Falling Leaves,UVa 699)
- 下落的树叶(The Falling Leaves)
- 例题6-10 UVa699 The Falling Leaves(二叉树)
- UVA699 The Falling Leaves【二叉树】【递归】
- Uva699——The falling leaves
- uva699 The Falling Leaves 二叉树
- The Falling Leaves 下落的树叶 UVA 699(二叉树)
- 例题6-10 下落的树叶(The Falling Leaves, UVa 699)
- Gradle files have changed since last project sync.问题的解决办法
- 免费的 webservice 接口服务地址
- ArrayList练习 ,去重
- Fragment生命周期
- 【Android - 框架】之ButterKnife的使用
- UVa699-The Falling Leaves(下落的叶子)
- 10分钟带你理解Java中的反射
- 【软考】软件攻城狮考试(十)——多媒体
- Linux下tomcat7与apache2整合
- php将数据写入数组
- 三步学会用spring开发OSGI——(第一步:环境篇)
- vcg数据结构2
- 拿铁11.13
- 位操作