POJ 题目1145/UVA题目112 Tree Summing(二叉树遍历)

Tree Summing

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8132   Accepted: 1949

Description

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.

Input

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.

Output

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.

Sample Input

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 ()

Sample Output

yes
no
yes
no

Source

Duke Internet Programming Contest 1992,UVA 112

题目大意:给出一个二叉树的表达式,问有没有一个路径和是n

ac代码

Problem: 1145		User: kxh1995
Memory: 164K		Time: 0MS
Language: C++		Result: Accepted
#include<stdio.h>
int tree_sum(int n)
{
	int ans=0,m;
	if(scanf(" (%d",&m))
	{
		ans=tree_sum(n-m)+tree_sum(n-m);
		if(ans<2)
			ans=0;
	}
	else
		ans=!n;
	scanf(" )");
	return ans;
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		if(tree_sum(n))
			printf("yes\n");
		else
			printf("no\n");
	}
}
时间: 2024-10-20 03:02:34

POJ 题目1145/UVA题目112 Tree Summing(二叉树遍历)的相关文章

UVA - 112 - Tree Summing (数的求和!栈的应用!)

UVA - 112 Tree Summing Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Tree Summing  Background LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the oldest

uva 112 - Tree Summing

 Tree Summing  Background 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 represe

UVa 112 - Tree Summing(树的各路径求和,递归)

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=48 Tree Summing  Background LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the olde

POJ - 2255 - Tree Recovery = 二叉树遍历

http://poj.org/problem?id=2255 题意:给定先序遍历和中序遍历,求后序遍历. 回忆以前上DataStructure课的时候貌似写过类似的. 先从先序入手,从左到右扫描,进入时节点时立刻入栈,离开节点时立刻出栈. 关键是怎么知道什么时候才是立刻节点了呢? 貌似只有n^2的做法,因为要从中序遍历序列中找根. 但其实假如预处理出中序遍历的序列中的字母每个指向的位置就不用这额外的时间花费n了. 也就是从先序遍历入手,进入节点时把根节点的中序遍历序列指针两侧递归下去. 所以构造

PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历

题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子.中序遍历的顺序正好是序列从小到大的顺序,因此中序遍历的时候顺便赋值就可以了,最后层次遍历输出. 思路一:中序遍历的时候赋值 #include <iostream> #include <cstdio> #include <algorithm> #include <str

PAT A1020 Tree Traversals [二叉树遍历]

题目描述 链接 给定后序和中序,求层序结果 分析 递归实现,这里给出先序和后序,求中序的 递归:考虑中间过程 先序\([preL,preR]\) 中序\([inL,inR]\) 根节点:\(preL\) 中序根节点:\(k,a[k]==a[preL]\) 遍历可得到 中序左子树结点个数:\(k-inL\) ,左子树序列\([inL,k-1]\) 先序左子树序列\([preL+1,preL+k-inL]\) 中序右子树结点个数:\(inR-k\) , 右子树序列\([k+1,inR]\) 先序右子

hdu1710(Binary Tree Traversals)(二叉树遍历)

Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3475    Accepted Submission(s): 1555 Problem Description A binary tree is a finite set of vertices that is either empty or

UVa536 Tree Recovery (二叉树遍历)

链接:http://acm.hust.edu.cn/vjudge/problem/19645分析:三种树的遍历方式中除了中序外再知道另外遍历方式得到的序列就可以唯一确定一棵二叉树.先序:先打印根再遍历左右子树.中序:先遍历左子树,然后打印根,再遍历右子树.后序:先遍历左右子树,最后打印根. 1 #include <cstdio> 2 #include <cstring> 3 4 char pre_order[30], in_order[30]; 5 6 void dfs(int L

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes