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 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
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
yes no yes no
#include <iostream>
#include <string>
using namespace std;
//递归扫描输入的整棵树
bool ScanTree(int nSum, int nDest, bool *pNull) {
static int nChild;
//略去当前一级前导的左括号
cin >> (char&)nChild;
//br用于递归子节点的计算结果,bNull表示左右子是否为空
bool br = false, bNull1 = false, bNull2 = false;
//如果读入值失败,则该节点必为空
if (!(*pNull = ((cin >> nChild) == 0))) {
//总和加上读入的值,遍例子节点
nSum += nChild;
//判断两个子节点是否能返回正确的结果
br = ScanTree(nSum, nDest, &bNull1) | ScanTree(nSum, nDest, &bNull2);
//如果两个子节点都为空,则本节点为叶,检验是否达到目标值
if (bNull1 && bNull2) {
br = (nSum == nDest);
}
}
//清除节点为空时cin的错误状态
cin.clear();
//略去当前一级末尾的右括号
cin >> (char&)nChild;
return br;
}
//主函数
int main(void) {
bool bNull;
//输入目标值
for (int nDest; cin >> nDest;) {
//根据结果输出yes或no
cout << (ScanTree(0, nDest, &bNull) ? "yes" : "no") << endl;
}
return 0;
}
(5(4(11(7()())(2()()))()) (8(13()())(4()(1()()))))它代表的树长这样:
![picture25](https://uva.onlinejudge.org/external/1/112img1.gif)
bool ScanTree(int nSum, int nDest, bool *pNull) {
static int nChild;
<span style="font-size: 13.3333px; font-family: Simsun;">cin >> (char&)nChild;</span>
。。。
if (!(*pNull = ((cin >> nChild) == 0))) {
。。。
br = ScanTree(nSum, nDest, &bNull1) | ScanTree(nSum, nDest, &bNull2);
。。。
}
。。。
}
<span style="color:#660000;">br = ScanTree(nSum, nDest, &bNull1) | ScanTree(nSum, nDest, &bNull2);</span>
这句话非常关键,相当于读入一个数据到当前结点后,再读入数据到它的左子树,再读入数据到它的右子树。
就是一个先序插入数据的过程
//二叉树结点类
class BSTNode {
public:
int element;
BSTNode*left;
BSTNode*right;
BSTNode() { left = NULL; right = NULL; }
BSTNode(const int& Element, BSTNode*Left, BSTNode*Right) {
element = Element;
left = Left;
right = Right;
}
~BSTNode() { delete left; delete right; }
};
class BST {
private:
BSTNode*root;//根节点
//一边读入数据一边按先序建立二叉树
BSTNode* build(BSTNode*& r) {
static int Child;
//忽略左括号
cin >> (char&)Child;
//读入数据
if (cin >> Child) {
r = new BSTNode(Child, NULL, NULL);
build(r->left);
build(r->right);
}
cin.clear();
//忽略右括号
cin >> (char&)Child;
return r;
}
//先序历遍
void preorder(BSTNode*r) {
if (r == NULL)return;
cout << r->element << " ";
preorder(r->left);
preorder(r->right);
}
public:
BST() { root = NULL; }
~BST() { delete root; }
void Build() {
build(root);
}
void pre() {
preorder(root);
}
};
#include"bst.h"
using namespace std;
int main() {
int value;
cin >> value;
BST* t = new BST();
t->Build();
t->pre();
}
输入:
输出:
<span style="white-space:pre"> </span>bool scanTree(int num, int Des, BSTNode*r) {
<span style="white-space:pre"> </span>bool b = false;
<span style="white-space:pre"> </span>if (r == NULL) return b;
<span style="white-space:pre"> </span>num += r->element;
<span style="white-space:pre"> </span>if (r->left == NULL&&r->right == NULL)
<span style="white-space:pre"> </span>b = ( num == Des )? true : false;
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>bool bl, br;
<span style="white-space:pre"> </span>bl = false;
<span style="white-space:pre"> </span>br = false;
<span style="white-space:pre"> </span>bl= scanTree(num, Des, r->left);
<span style="white-space:pre"> </span> br=scanTree(num, Des, r->right);
<span style="white-space:pre"> </span>b = bl | br;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return b;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>bool ScanTree(int num, int Des) {
<span style="white-space:pre"> </span>return scanTree(num, Des, root);
<span style="white-space:pre"> </span>}
修改主程序如下:
#include"bst.h"
using namespace std;
int main() {
int value;
FILE* stream;
freopen_s(&stream, "C:\\Users\\zgwng\\Desktop\\112data.txt", "r", stdin);
while (cin >> value) {
BST* t = new BST();
t->Build();
bool result = t->ScanTree(0, value);
if (result) cout << "yes" << endl;
else
cout << "no" << endl;
}
}
测试数据集:
20 (5(4(11(7()())(2()()))()) (8(13()())(4()(1()()))))
10 (3
(2 (4 () () )
(8 () () ) )
(1 (6 () () )
(4 () () ) ) )
5 ()
强制转换会产生一个临时变量,设为temp, (char)nChild会产生一个char temp,使其值等于nChild,所以cin >> (char)nChild相当于cin >> temp,而临时变量是不能作为cin的参数的,所以编译会失败(排除nChild是char类型这种情况); (char&)nChild会产生一个char& temp = nChild,所以cin >> (char&)nChild相当于cin >> temp,temp是对nChild的引用,所以实际作为cin参数的还是nChild,因此会编译通过。
BSTNode* build(BSTNode* r)
BSTNode* build(BSTNode*& r) {
//一边读入数据一边按先序建立二叉树
BSTNode* build(BSTNode*& r) {
static int Child;
//忽略左括号
cin >> (char&)Child;
//读入数据
if (cin >> Child) {
r = new BSTNode(Child, NULL, NULL);
build(r->left);
build(r->right);
}
cin.clear();
//忽略右括号
cin >> (char&)Child;
return r;
}
<span style="white-space:pre"> </span>bool scanTree(int num, int Des, BSTNode*r) {
<span style="white-space:pre"> </span>num += r->element;
<span style="white-space:pre"> </span>if (r->left == NULL&&r->right == NULL)
<span style="white-space:pre"> </span> return ( num == Des )? true : false;
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span> if(r->left!=NULL) return scanTree(num, Des, r->left);
<span style="white-space:pre"> </span> if (r->left != NULL)return scanTree(num, Des, r->right);
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>}
这样做的结果是只要第一条路返回了false,最后结果就是false
bool scanTree(int num, int Des, BSTNode*r) {
bool b = false;
if (r == NULL) return b;
num += r->element;
if (r->left == NULL&&r->right == NULL)
b = ( num == Des )? true : false;
else
{
bool bl, br;
bl = false;
br = false;
bl= scanTree(num, Des, r->left);
br=scanTree(num, Des, r->right);
b = bl | br;
}
return b;
}