uva112-Tree Summing[动态建树]

总时间限制: 
1000ms 
内存限制: 
65536kB
描述
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. 
输入
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.
输出
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.
样例输入
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 ()
样例输出
yes
no
yes
no

思路:动态建树,有两点需要注意:

①整数可能是负数

②0 ()  结果为:no


AC代码:

#include <cstdio>
using namespace std;
const int MAXN = 100005;
struct Node {
    int value;
    Node *left, *right;
    Node() {
        value = 0;
        left = NULL;
        right = NULL;
    }
};
Node *root;
int num;
int sum[MAXN], tot;
Node* build() {
    char ch;
    int cnt = 0;
    int val = 0;
    bool negative = false;
    Node *rt = new Node();
    while (scanf("%c", &ch) != EOF) {
        if ('0' <= ch && ch <= '9') {
            val = val * 10 + (ch - '0');
        } else if (ch == '(' && cnt == 0) {
            rt->left = build();
            cnt++;
        } else if (ch == '(' && cnt == 1) {
            rt->right = build();
            cnt++;
        } else if (ch == '-') {
            negative = true;
        } else if (ch == ')') {
            break;
        }
    }
    if (negative) {
        val = -val;
    }
    rt->value = val;
    if (cnt == 0 && val == 0) {
        return NULL;
    }
    return rt;
}
void pre(int s, Node *rt) {
    if (rt->left == NULL && rt->right == NULL) {
        s += rt->value;
        sum[tot++] = s;
        return ;
    }
    if (rt->left != NULL) {
        pre(s + rt->value, rt->left);
    }
    if (rt->right != NULL) {
        pre(s + rt->value, rt->right);
    }
}
Node* dele(Node *rt) {
    if (rt->left == NULL && rt->right == NULL) {
        delete rt;
        return NULL;
    }
    if (rt->left != NULL) {
        rt->left = dele(rt->left);
    }
    if (rt->right != NULL) {
        rt->right = dele(rt->right);
    }
    return rt;
}
int main() {
    while (scanf("%d", &num) != EOF) {
        root = NULL;
        char ch;
        while (scanf("%c", &ch) != EOF) {
            if (ch == '(') {
                root = build();
                break;
            }
        }
        tot = 0;
        if (root != NULL) {
            pre(0, root);
        }
        bool tag = false;
        for (int i = 0; i < tot; i++) {
            if (sum[i] == num) {
                tag = true;
                break;
            }
        }
        if (tag) {
            printf("yes\n");
        } else {
            printf("no\n");
        }
        if (root != NULL) {
            dele(root);
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值