Tree Summing UVA - 112 二叉树

问题:

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

                                                                  Tree Summing UVA - 112   二叉树
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

题目:用括号方式给你一颗二叉树,一个预期的值result,如果从根到叶子节点所有的节点

的值的和能够等于result,则输出yes,否则输出no.

思路:建树,然后用bfs搜一遍即可;注意:输入很麻烦,还要考虑负数的情况,做了一上

午就是因为负数没考虑到一直WA;代码下;

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
    int v,sum;
    node *l,*r;
};
int s[120000],vis[120000],n;
node* build(int &x,node *cur)//建树算是模板
{
    int h=s[x++];
    if(vis[x-1]==1)
        return NULL;
    if(cur==NULL)
    {
        node *root=new node;
        root->v=h;
        root->sum=0;
        root->l=root->r=NULL;
        root->l=build(x,root->l);
        root->r=build(x,root->r);
        return root;
    }
}
void find1(node *p)//很简单的bfs
{
    queue<node*>q;
    while(!q.empty())
        q.pop();
    p->sum=p->v;
    q.push(p);
    while(!q.empty())
    {
        node *cur=q.front();
        q.pop();
        if(cur->r==NULL&&cur->l==NULL&&cur->sum==n)
        {
            printf("yes\n");
            return ;
        }
        int b=cur->sum;
        if(cur->l!=NULL)
        {
            node *w=cur->l;
            w->sum=b+w->v;
            q.push(w);
        }
        if(cur->r!=NULL)
        {
            node *w=cur->r;
            w->sum=b+w->v;
            q.push(w);
        }
    }
    printf("no\n");
    return ;
}
int main()
{
    while(~scanf("%d",&n))
    {
        memset(s,0,sizeof(s));
        memset(vis,0,sizeof(vis));
        int flag=0,t=0,k=0,tag=0,ans=0;
        char c;
        while(~scanf("%c",&c))//输入
        {
            if(c==' '||c=='\n')
                continue;
            if(c=='-')//负数情况,就是这个地方一直错,
                tag=1;
            else if('0'<=c&&c<='9'&&flag==1)
                ans=ans*10+c-48;
            else if('0'<=c&&c<='9')
            {
                flag=1;
                ans=ans*10+c-48;
            }
            else if(c=='('&&flag==1&&tag==1)
            {
                s[t]=ans*(-1);
                tag=0;
                ans=0;
                t=t+1;
                flag=0;
                k=k+1;
            }
            else if(c=='('&&flag==1)
            {
                s[t]=ans;
                ans=0;
                t=t+1;
                flag=0;
                k=k+1;
            }
            else if(c==')'&&flag==0)
            {
                flag=1;
                s[t]=0;
                vis[t]=1;
                k=k-1;
            }
            else if(c=='(')
                k=k+1;
            else if(c==')')
                k=k-1;
            if(k==0)
                break;
        }
        if(vis[0]==1)
        {
            printf("no\n");
            continue;
        }
        node *kk=NULL;
        int p=0;
        kk=build(p,kk);
        find1(kk);
    }
    return 0;
}

 

上一篇:UVA 548 Tree


下一篇:BeautifulSoup爬网页图片