112 - Tree Summing

来源:互联网 发布:最小公倍数算法流程图 编辑:程序博客网 时间:2024/05/01 21:44


 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.

picture25

Binary trees are represented in the input file as LISP S-expressions having the following form.

 empty tree  ::=  ()

tree ::= empty tree tex2html_wrap_inline118 (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

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 pairI,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

yesnoyesno

分析:题目考察的是树的二叉树的遍历,可以使用前序遍历的思想解决这个问题。不过对于题目需要注意几个问题,第一:空树()时输出no。第二:递归过程中,当遇到叶子节点时才将路径上节点的累积和与给定数比较,衍生出一个问题就是:如何判断一个结点是叶子结点。第三:结点的值可能是负数(因为没注意这点,我硬是提交了10多遍才过的。。。)。

这里补充一些输入案例(是在豆丁上找到的。。。)

/* 22 (7()(6(5(4(11(7()())(2()()))())(8(13()())(4()(1()()))))())) 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 () () ) ) ) ) )*/

代码:

#include <iostream>#include <string>#include <string.h>#include <stack>#include <cctype>#include <sstream>using namespace std;int get_root_value( string &tree_exp,int start){//取得根结点的值string number;size_t i=start+1;//忽略左括号for(;i<tree_exp.size();++i){if(tree_exp[i]!='(') number.push_back(tree_exp[i]);else break;}istringstream istream(number);int value=0;istream>>value;return value;}int get_left_start( string &tree_exp,int start){//左子树的开始位置size_t i=start+1;//忽略做括号for(;i<tree_exp.size();++i){if(tree_exp[i]=='(') break;}return i;}int get_left_end( string &s,size_t left_start){//返回左子树的结束位置stack<int> stk;size_t i=left_start;for(;i<s.size();++i){if(s[i]=='(') stk.push(i);else if(s[i]==')') stk.pop();if(stk.empty()){break;}}return i;}string tree_exp;bool is_exist(int dest_sum,int sum,bool &is_null,int start,int end){//dest_sum表示指定的和,sum表示每次递归时计算出的和,is_null表示当前节点是否为空,[start,end)表示子树范围 if(end-start==1){//当前子树是否为空 is_null=true; return false; } int root_value=get_root_value(tree_exp,start); sum+=root_value;//累加和 int left_start=get_left_start(tree_exp,start); int left_end=get_left_end(tree_exp,left_start); bool left_null=false; bool left=is_exist(dest_sum,sum,left_null,left_start,left_end);//递归判断左子树 int right_start=left_end+1; int right_end=end-1; bool right_null=false; bool right=is_exist(dest_sum,sum,right_null,right_start,right_end);//递归判断右子树 if(left_null && right_null){//当前节点的左右子树都为空    if(sum==dest_sum) return true;else return false; }  return left|right;}int main(){int dest_sum;while(cin>>dest_sum){stack<char> stk;char c;while(cin>>c){//预处理输入,去掉空格,去掉换行if(c=='(') stk.push('(');else if(c==')') stk.pop();tree_exp.append(1,c);if(stk.empty()) break;}//cout<<tree_exp<<endl;int sum=0;bool is_null=false;if(is_exist(dest_sum,sum,is_null,0,tree_exp.size()-1)){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}tree_exp="";}return 0;}

0 0
原创粉丝点击