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 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
#include <iostream> #include <stack> #include <cstring> #include <cstdio> #include <string> #include <algorithm> #include <queue> #include <set> #include <map> #include <fstream> #include <stack> #include <list> #include <sstream> using namespace std; #define ms(arr, val) memset(arr, val, sizeof(arr)) #define N 1000000 #define INF 0x3fffffff #define vint vector<int> #define setint set<int> #define mint map<int, int> #define lint list<int> #define sch stack<char> #define qch queue<char> #define sint stack<int> #define qint queue<int> struct node { int val; int l; int r; int tag;//建树标志 void set(int val) { l = r = 0; tag = 2; this->val = val; } }tree[N]; char seq[N]; int val, root, p, I; sint si; int t; bool ldigit()//左括号右边是数字,并且移动到下一个左括号位置,如果存在的话 { t++; if (isdigit(seq[t]))//正数 { val = seq[t] - ‘0‘; while (isdigit(seq[++t]))//得到数字 { val = val * 10 + seq[t] - ‘0‘; } return true; } if (seq[t] == ‘-‘)//负数 { val = 0; while (isdigit(seq[++t]))//得到数字 { val = val * 10 + seq[t] - ‘0‘; } val = -val; return true; } while (seq[++t] && seq[t] != ‘(‘); return false; } void getSeq()//读取输入,注意还存在负数的情况,一开始没考虑到 { int con = 1, n = 0; char ch; while (ch = getchar(), ch != ‘(‘); seq[n++] = ‘(‘; while (con) { ch = getchar(); if (ch == ‘(‘) { con++; seq[n++] = ch; } else if (ch == ‘)‘) { con--; seq[n++] = ch; } else if (isdigit(ch)) { seq[n++] = ch; } else if (ch == ‘-‘) { seq[n++] = ch; } } seq[n] = ‘\0‘; t = 0; } void build()//建树 { root = 0; if (ldigit()) { tree[++root].set(val); si.push(root); } int pos, tmp; while (!si.empty()) { if (ldigit()) { tree[++root].set(val); pos = root; } else pos = 0; tmp = si.top(); if (tree[tmp].tag == 2)//左子树 { tree[tmp].l = pos; tree[tmp].tag--; } else//右子树 { tree[tmp].r = pos; si.pop(); } if (pos) { si.push(pos); } } } bool dfs(int i) { bool tag = false; val += tree[i].val; if (tree[i].l || tree[i].r) { if (tree[i].l && dfs(tree[i].l)) { return true; } if (tree[i].r && dfs(tree[i].r)) { return true; } } else { tag = I == val ? true : false; } val -= tree[i].val;//放错位置了 return tag; } int main() { //freopen("in.in", "r", stdin); while (~scanf("%d", &I)) { getSeq(); build(); val = 0; if (root && dfs(1)) puts("yes"); else puts("no"); } return 0; }
uva 112 - Tree Summing,布布扣,bubuko.com
原文:http://www.cnblogs.com/jecyhw/p/3903010.html