poj 1145 tree summing

Tree Summing
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7866 Accepted: 1815

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 ()
先建树,然后用BFS算和。
最纠结的部分还是输入.....

Sample Output

yes
no
yes
no
#include <iostream>
#include<deque>
#include<algorithm>
#include<cstdio>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<sstream>
#include<cctype>
#include<queue>
#include <cstring>
#include <cstdlib>
#include <fstream>
using namespace std;
int countc=0,count1,count2;
char str[10010];
//ifstream fin("ha.txt");
struct nodeT{
    bool swi;
    int value;
    nodeT *left,*right;
};
nodeT *leaf(){
    nodeT *nod=new nodeT;
    nod->left=NULL;nod->right=NULL;
    return nod;
}
nodeT *build(){
    nodeT *sta[10010],*temp,*root,*le,*ri;
    int total=0,num,i=0;
    while(i<countc){
        if(str[i]=='('){
                temp=leaf();
                temp->swi=0;
                sta[total++]=temp;
                i++;
           }
        else if((str[i]>='0'&&str[i]<='9')||str[i]=='-'){
                bool minus=false;
                if(str[i]=='-'){i++;minus=true;}
                num=0;
                while(str[i]>='0'&&str[i]<='9')
                {
                    num=num*10+str[i]-'0';i++;
                }
                if(minus)num*=-1;
                temp=leaf();
                temp->swi=1;
                temp->value=num;
                sta[total++]=temp;
           }
        else{
            bool flag=1;
            if(sta[total-1]){
            if(sta[total-1]->swi==0)
                {
                sta[total-1]=NULL;
                i++;
                flag=0;
                }
            }
            if(flag==1){
                //最后要把生成的放到sta[total-4]里面。total-=3;
                ri=sta[total-1];le=sta[total-2];root=sta[total-3];
                root->left=le;root->right=ri;
                sta[total-4]=root;
                total-=3;
                i++;
            }
        }
    }
    return sta[total-1];
}
void input()
{
//读入数据,只要数,括号和负号

    int flag = 0 ;
    while( 1 )
    {
        char ch = cin.get() ;
        if( ch =='(' )
        {
            flag = 1 ;
            count1++ ;
            str[countc++] = ch ;
        }
        if( isdigit( ch ) || ch == '-' )
        {
            str[countc++] = ch ;
        }
        if( ch == ')' )
        {
            count2++ ;
            str[countc++] = ch ;
        }
        if( count1  == count2  && flag ) break ;//如果前括号与后括号的数量相同,并且进入过括号,读入数据结束
    }
    return ;
}


int main()
{
    int ans;
    nodeT *root,*temp;
    while(cin>>ans){
        countc=0;count1=0;count2=0;
        int answer[10005],cnt=0;
        input();
        //for(int i=0;i<countc;i++)cout<<str[i];
        root=build();
        queue<nodeT *>que;
        if(root!=NULL)que.push(root);
        while(!que.empty()){
            temp=que.front();
            que.pop();
            //cout<<temp->value<<" ";
            bool pig=true;
            if(temp->left){temp->left->value+=temp->value;que.push(temp->left);pig=false;}
            if(temp->right){temp->right->value+=temp->value;que.push(temp->right);pig=false;}
            if(pig){answer[cnt++]=temp->value;}
        }
        bool find=false;
        for(int i=0;i<cnt;i++)
        {
            //cout<<answer[i]<<" ";
            if(answer[i]==ans){find=true;break;}
        }
        if(find){cout<<"yes"<<endl;}
        else{cout<<"no"<<endl;}
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值