UVA - 112 Tree Summing (难在输入)

  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 on the right 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

题目大意:从树的根节点到叶节点的权值和是否能与给定的权值相等,可以为yes,否则no。

思路: 难在对输入上的处理,第一次碰到这样的输入。题意很简单,规定没有节点的地方值为inf,用队列储存节点的值,依次取出,根据前序遍历将树建立起来,然后dfs访问根节点到叶节点的和。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<iostream>
using namespace std;
#define inf 0x3f3f3f3f
queue<int>q;
int only;
int n;
struct node
{
    int data;//当前的根节点
    node *lson;
    node *rson;
    node()//构造函数
    {
        lson=rson=NULL;
    }
};
node *inser(node *root)
{
    int x=q.front();//取出当前的值
    q.pop();
    if(x==inf)//值为inf,当前节点为空,返回
    {
        root=NULL;
        return root;
    }
    node *p=new node;//申请空间
    p->data=x;
    p->lson=inser(p->lson);//访问左子树
    p->rson=inser(p->rson);//访问右子树
    return p;
}
int judge(node *root,int sum)
{
    if(root!=NULL)//当前节点不为空
    {
        int x=judge(root->lson,sum+root->data);//左
        int y=judge(root->rson,sum+root->data);//右
        if(x+y==2)//左右儿子都没有,即叶子节点
        {
            if(sum+root->data==n)
                only=1;
        }
        return 0;
    }
    else
        return 1;//为空返回1
}
int main()
{
    while(~scanf("%d",&n))
    {
        vector<char>v;
        while(!q.empty())
            q.pop();
        char c;
        int l=0,r=0;

        while(~scanf("%c",&c))//一个一个字符输入
        {
            if((c>='0'&&c<='9')||(c=='(')||(c==')')||(c=='-'))//数据为整形,即可能为负数
            {
                v.push_back(c);
                if(c=='(')
                    l++;//左
                if(c==')')
                    r++;//右
                if(l==r)
                    break;
            }
        }
        for(int i=0; i<v.size(); i++)//遍历
        {
            if(v[i]=='(')//左括号后即为数据
            {
                int p=0,num=0,flag=0;
                for(++i; i<v.size(); i++)//**************
                {
                    if(v[i]=='-')
                        flag=1;
                    else if(v[i]>='0'&&v[i]<='9')
                    {
                        num++;//数的位数
                        p=p*10+v[i]-48;
                    }
                    else
                        break;
                }
                if(num==0)
                    q.push(inf);//没有数据
                else//**************
                {
                    if(flag)
                        p=-p;//负数
                    q.push(p);
                }
                i--;//下次可能还需要用到'('*******************
            }
        }
        node *root=NULL;
        root=inser(root);//插入数据
        only=0;//
        judge(root,0);//

        if(only)
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值