首页 > 其他分享 >UVa 112 Tree Summing (scanf()去空格&递归&二叉树遍历)

UVa 112 Tree Summing (scanf()去空格&递归&二叉树遍历)

时间:2023-04-12 13:03:08浏览次数:56  
标签:input no int Tree scanf tree 二叉树 ans integer


112 - Tree Summing

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=48

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 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.

The Problem

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

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.

The Output

There should be one line of output for each test case (integer/tree pair) in the input file. For each pairI,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



看代码你就懂了。


完整代码:

/*0.082s*/

#include<cstdio>

int f(int n)
{
	int ans, m;
	if (scanf(" (%d", &m))
	{
		ans = f(n - m) + f(n - m);
		if (ans < 2) ans = 0;
	}
	else ans = !n;
	scanf(" )");
	return ans;
}

int main()
{
	int n;
	while (~scanf("%d", &n))
		///空树会返回1
		puts(f(n) > 1 ? "yes" : "no");
	return 0;
}




标签:input,no,int,Tree,scanf,tree,二叉树,ans,integer
From: https://blog.51cto.com/u_5535544/6185490

相关文章

  • UVa 143 Orchard Trees (数学&计算几何&枚举)
    143-OrchardTreesTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=79AnOrchardisthasplantedanorchardinarectanglewithtreesuniformlyspacedinbothdirections.T......
  • TreeMap运行错误
    Exceptioninthread"main"java.lang.ClassCastException:Day16_TreeMap.Starcannotbecasttojava.lang.Comparable提示类型转换错误,publicstaticvoidmain(String[]args){TreeMap<Star,String>Tr=newTreeMap<Star,String>();//下句出错......
  • 利用pandas 和 ttk.Treeviews制作xlsx视图工具
     importtkinterastkfromtkinterimportttkimportpandasaspdimporttkinter.messageboxasmsgboxdefStart():msgbox.showinfo('提示','OK')fp=pd.read_excel("./test.xlsx")foriintree.get_children():......
  • 基于elementui的Tree虚线,实线绘制,以及懒加载,如图
     加减号用的是阿里的矢量图标库。自行去下载 路径:https://www.iconfont.cn/home/index?spm=a313x.7781069.1998910419.2 <template><divclass="content-box"><divclass="content-top-lable">系统设置</div><divstyle="padd......
  • el-tree 的样式修改之旅
    原始样式如下图:  我想要实现的样子如下图:  首先分析一下想要实现的效果图:1,想要从原始效果到效果图的样子都需要修改那些地方  2,都涉及到那些样式问题其次简要说一下本人在实践中都遇到了那些样式问题:1,修改el-tree中checkbox选中时......
  • 修改el-tree checkbox和文字大小
    原始效果: 需求描述:默认效果如上图,想改变复选框的大小,方便使用者勾选,需要修改el-tree checkbox和文字的大小<el-checkboxclass="el-checkbox"label=""v-model="item.check"></el-checkbox>主要代码是css:.el-checkbox{zoom:130%;//适配谷歌......
  • 二叉树的顺序存储
    二叉树的顺序存储二叉树的存储形式按照二叉树的结点层次编号,然依次后储存在数组当中二叉树的抽象数据类型表示二叉树顺序存储结构的示意图例题二叉树顺序存储结构的缺点1.顺序存储结构的大小固定不能动态的变化2.如果如图上为右单支树一样浪费空间所以顺序存储结构......
  • Git 工具 - 子模块: submodule与subtree的使用
    git日常使用中,基本都是一个项目一个Git仓库的形式,那么当我们的代码中碰到了业务级别的需要复用的代码,我们一般怎么做呢?比如:某个工作中的项目需要包含并使用另一个项目。也许是第三方库,或者你独立开发的,用于多个父项目的库。所以需要提取一个公共的类库提供给多个项目使用,但是......
  • 判断二叉树是否对称
    递归遍历recursion(root1,root2){if(root1==null&&root2== null){returnture;}if(root1==null||root2==null||root1.val!=root2.val)returnfalse;returenrecursion(root1.left,root2.right)&&recursion(root2.left,root2.right);......
  • 搜索二叉树转换成双向链表
    搜索二叉树:每个节点的左子树的值都小于当前节点,右子树的节点值都大于当前节点。其中序遍历就是一个有序的序列转化成双向链表,需要记录一下头节点,和前一个节点,将前一个节点和当前节点相连preheadconvert(pRoot){if(pRoot==null)returnnull;convert(pRoot.left);......