Problem Links:
uva00112, poj1145,Problem:
Tree Summing
Tree Summing |
Background
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.
The Problem
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 treeThe tree diagrammed above is represented by the expression (5 (4 (11 (7 () ()) (2 () ()) ) ()) (8 (13 () ()) (4 () (1 () ()) ) ) )(integer tree tree)
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
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.The 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, Trepresents 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 isI.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
Solution:
Using recursive to avoid building the tree;
1st, there exists negative numbers in the input file;
2nd, the empty tree can't have a root to leaf path.
3rd, the empty subtree can't have a root to leaf subpath.
Test
Input file:
Output file:
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 () 0 () 5 (5 () ()) 5 ( 5 () () ) 5 (1 (3 () ()) (4 () ())) 5 (18 ( - 13 ( ) ( ))()) 0 (1 ()(-2 () (1()()) ) ) 2 (1 () (1 () (1 () () ) ) ) 10 (5 () (5 () (5 () (5 () (4 () () ) ) ) ) ) 10 (5 () (5 () (5 () (5 ( 3 () () ) (4 () () ) ) ) ) ) 20 (5 () (5 () (5 () (5 () (4 () () ) ) ) ) )
yes
no
yes
no
no
yes
yes
yes
yes
yes
no
no
no
no
Source Code:
//Sun Apr 24 12:47:06 CDT 2011
#include <vector>#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int total; //The summation;
bool flag; //If there exists a path from root to leaf.
bool mark; //Check if tree is empty;
int recursive(int sum) {
char ch;
int number;
int leaf = 0;
do {
cin >> ch;
} while (ch == ' ' || ch == '\n');
// cout << ch;
if (isdigit(ch)) {
mark = true;
cin.unget();
cin >> number;
leaf += recursive(sum + number);
leaf += recursive(sum + number);
do {
cin >> ch;
} while (ch != ')');
// cout << number << ", " << leaf << endl;
if (leaf == 2 && sum + number == total)
flag = true;
return 0;
} else if (ch == '-') {
mark = true;
cin >> number;
leaf += recursive(sum - number);
leaf += recursive(sum - number);
do {
cin >> ch;
} while (ch != ')');
// cout << number << ", " << leaf << endl;
if (leaf == 2 && sum - number == total)
flag = true;
return 0;
} else if (ch == '(')
recursive(sum);
else if (ch == ')') {
return 1;
}
}
int main() {
// freopen("input.in", "r", stdin);
while (cin >> total) {
flag = false;
mark = false;
recursive(0);
if (flag && mark)
cout << "yes" << endl;
else
cout << "no" << endl;
}
// fclose(stdin);
return 0;
}
No comments :
Post a Comment