UVA Tree Summing

题目如下:

 Tree Summing 

Background

LISP was one of the earliest high-level programming languages and, withFORTRAN, is one of the oldest languages currently being used. Lists,which are the fundamental data structures in LISP, can easily be adaptedto represent other important data structures such as trees.

This problem deals with determining whether binary trees represented asLISP S-expressions possess a certain property.

The Problem

Given a binary tree of integers, you are to write a program thatdetermines whether there exists a root-to-leaf path whosenodes sum to a specified integer. For example, in the tree shown belowthere are exactly four root-to-leaf paths.The sums of the paths are 27, 22, 26, and 18.

picture25

Binary trees are represented in the input file as LISP S-expressionshaving the following form.

 
empty tree 		 ::= 		 ()

tree ::= empty tree tex2html_wrap_inline118 (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 apath exists whose sum is a specified integer in an empty treemust be answered negatively.

The Input

The input consists of a sequence of test cases in theform of integer/tree pairs. Each test caseconsists of an integer followed by one or more spaces followed by abinary tree formatted as an S-expression as described above. Allbinary tree S-expressions will be valid, but expressions may bespread over several lines and may contain spaces.There will be one or more test cases in an input file, and input isterminated by end-of-file.

The Output

There should be one line of output for each test case (integer/treepair) in the input file. For each pair I,T (I represents theinteger, T represents the tree) the output is the string yes ifthere is a root-to-leaf path in T whose sum is I and no ifthere 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


这道题我是先建树,再遍历。难点在于建树,因为输入比较麻烦,可以分几行输入,所以对空格要忽略,只有对空树的时候才忽略前导‘(’,若非空树,每遇到一个‘(’代表一个节点成功输入。

AC的代码如下;

#include
   
   
    
    
using namespace std;
struct BINTREE
{
    int data;
    BINTREE* lchild,*rchild;
};
bool flag=false;
void creattree(BINTREE*&T)
{
    int v=0,form=0;
    char c;
    if( !flag ) while( getchar() != '(' );
    while(c=getchar())
    {
        if(c=='-')
        {
            form=1;
            continue;
        }
        else if(isspace(c))
            continue;
        else if(c=='('||c==')')
            break;
        else if(isdigit(c))
            v=v*10+c-'0';
    }
    if(c==')')
    {
        T=NULL;
        flag=false;
    }
    else
    {
        flag=true;
        T=new BINTREE;
        if(form)
            v=-v;
        T->data=v;
        creattree(T->lchild);
        creattree(T->rchild);

    }
}
void traveltree(BINTREE* T,int ans)
{
    if(!T)
        return;
    if(flag)
        return;
    ans=ans-T->data;

    if(T->lchild) traveltree(T->lchild,ans);
    if(T->rchild) traveltree(T->rchild,ans);
    if(!ans&&!T->lchild&&!T->rchild)
    {
        flag=true;


    }

}
int main()
{
    int sum;
    char c;
    while(scanf("%d",&sum)!=EOF)
    {
        BINTREE *T;
        flag=false;
        creattree(T);
        while(c=getchar())
        {
            if(c==EOF)
                break;
            if(c=='\n')
                break;
        }
        traveltree(T,sum);
        if(flag)
            cout<<"yes"<
    
    
   
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值