2649: Tree Summing
来源:互联网 发布:朱仙镇木版年画 淘宝 编辑:程序博客网 时间:2024/05/17 07:18
题目描述
LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the oldest languages currently being used. Lists, which are the fundamental data structures in LISP, can easily be adapted to represent other important data structures such as trees.
This problem deals with determining whether binary trees represented as LISP S-expressions possess a certain property.
Given a binary tree of integers, you are to write a program that determines whether there exists a root-to-leaf path whose nodes sum to a specified integer. For example, in the tree shown below there are exactly four root-to-leaf paths. The sums of the paths are 27, 22, 26, and 18.
Binary trees are represented in the input file as LISP S-expressions having the following form.
empty tree ::= ()
tree ::= empty tree (integer tree tree)
The tree diagrammed above is represented by the expression (5 (4 (11 (7 () ()) (2 () ()) ) ()) (8 (13 () ()) (4 () (1 () ()) ) ) )
Note that with this formulation all leaves of a tree are of the form (integer () () )
Since an empty tree has no root-to-leaf paths, any query as to whether a path exists whose sum is a specified integer in an empty tree must be answered negatively.
输入
The input consists of a sequence of test cases in the form of integer/tree pairs. Each test case consists of an integer followed by one or more spaces followed by a binary tree formatted as an S-expression as described above. All binary tree S-expressions will be valid, but expressions may be spread over several lines and may contain spaces. There will be one or more test cases in an input file, and input is terminated by end-of-file.
输出
There should be one line of output for each test case (integer/tree pair) in the input file. For each pair I,T (I represents the integer, T represents the tree) the output is the string yes if there is a root-to-leaf path in T whose sum is I and no if there is no path in T whose sum is I.
样例输入
22 (5(4(11(7()())(2()()))()) (8(13()())(4()(1()()))))
20 (5(4(11(7()())(2()()))()) (8(13()())(4()(1()()))))
10 (3
(2 (4 () () )
(8 () () ) )
(1 (6 () () )
(4 () () ) ) )
5 ()
样例输出
yes
no
yes
no
#include<iostream> using namespace std;typedef struct tree{ int data; int sum; struct tree *lChild; struct tree *rChild;}tree;int nums;bool flag;tree* newNode() { tree* u = (tree*)malloc(sizeof(tree)); u->lChild = u->rChild = NULL; u->data = u->sum = 0; return u;}tree* inputT(int t) { char ch; int v; cin >> ch; tree* u = newNode(); if (!(cin >> v)==0) {//输入错误(非int)不进入 u->data = v; u->sum = t + v; u->lChild = inputT(u->sum); // inputT(u->lChild, u->sum); 传u->lChild不会改变u->lChild的值,因为是同一级指针 u->rChild = inputT(u->sum); cin >> ch; } else { cin.clear();//清空错误,重新输入 cin >> ch; u = NULL; } if (u!=NULL) {// cout << u->sum << endl << u->lChild << endl << u->rChild << endl; if (u->sum == nums && u->lChild == NULL && u->rChild == NULL) { flag = true; } } return u;}void Preorder(tree *u) { if (u!=NULL) { cout<<u->data<<" "<<u->sum<<"! "; Preorder(u->lChild); Preorder(u->rChild); } } int main(int argc, char *argv[]){ while (cin >> nums) { flag = false; tree* t = NULL; t = inputT(0); // Preorder(t); if (flag) { cout << "yes" << endl; } else { cout << "no" << endl; } } return 0;}
- 2649: Tree Summing
- Tree Summing
- POJ 1145 Tree Summing
- poj 1145 Tree Summing
- 112 - Tree Summing***
- UvaOJ 112 - Tree Summing
- uva 112 Tree Summing
- uva 112 - Tree Summing
- uva 112 - Tree Summing
- UVa 112 - Tree Summing
- 112 - Tree Summing
- uva 112 - Tree Summing
- UVA112-Tree Summing
- uva 112 Tree Summing
- uva_112-Tree Summing
- UVA 112 Tree Summing
- UVa 112 - Tree Summing
- UVaOJ112---Tree Summing
- 英寸、英尺——英制度量
- [Linux]压缩/解压缩RAR文件
- Detected memory leaks!
- DataLoad命令集
- glog功能介绍
- 2649: Tree Summing
- SpssModeler记录、字段的操作说明
- 冀人社发[2012]40号企业职工…
- 冀人社发〔2015〕15号关于印发《河…
- VC开发的ocx调用dll中报错“0x80010012 被呼叫方(服务器 [不是服务器应用程序])不可用并已消失。所有连接均无效。没有执行呼叫。”
- 冀人社发[2012]40号关于《社会保险…
- diesel单词由来
- 传统微信营销的弊端
- 中国古代政权割据