现在的位置: 首页 > 综合 > 正文

112 – Tree Summing

2018年04月23日 ⁄ 综合 ⁄ 共 2878字 ⁄ 字号 评论关闭

 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

yes
no
yes
no

题意:一颗二叉树,用()表示一颗空树,(数字,()())来表示只有根节点的树,依次类推来表示这棵二叉树。

然后给你一个数,问是否能够从根节点开始到叶子的路径上的所有数字相加等于该数。


想法:这几天刚刚接触树。有点被搞得云里雾里的。

所以这道题其实是不需要建树的,不过我还是建立了树。也算是再巩固巩固。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
struct tree
{
    int data;
    int sum;
    tree* lchild;
    tree* rchild;
};//树的结点,用data来记录结点内容,用sum来记录从根节点到该节点的总数
tree* newnode()
{
    tree* u=(tree*) malloc (sizeof(tree));
    u->lchild=u->rchild=NULL;
    u->data=u->sum=0;
    return u;
}//建立一个新的结点
int n;
bool flag=false;
tree* input(tree * u, int t)
{
    char ch;
    int v;
    cin>>ch;//用来接收(
    u=newnode();
    if (!(cin>>v)==0)
    {
        u->data=v;
        u->sum=t+v;
        u->lchild=input(u->lchild,u->sum);
        u->rchild=input(u->rchild,u->sum);
        cin>>ch;//接收)
    }
    else
    {
        cin.clear();//这个函数能够将cin的错误信息清空
        cin>>ch;
        u=NULL;
    }
    if (u!=NULL)//判断是否满足条件
    {
        if (u->sum==n && !u->lchild && !u->rchild) flag=true;
    }
    return u;
}
void Preorder(tree *u)//这个前序遍历可以无视
{
    if (u!=NULL)
    {
        cout<<u->data<<" "<<u->sum<<"! ";
        Preorder(u->lchild);
        Preorder(u->rchild);
    }
}
int main ()
{
    int i,j,t;
    while(cin>>n)
    {
        flag=false;
        front=0;
        tree *root=newnode();
        root=input(root,0);
        if (flag) cout<<"yes"<<endl;
        else cout<<"no"<<endl;
    }
    return 0;
}

抱歉!评论已关闭.