UVa 112:tree Summing two-fork tree construction, Binary tree path and

Source: Internet
Author: User

Topic Link:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category= &problem=48&mosmsg=submission+received+with+id+10280379

Type of topic: data structure, two fork tree

Enhanced version 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 ()
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 () ())))

Enhanced Sample output:

Yes
No
Yes
No
No
Yes
Yes
Yes
Yes
Yes
No
No
No
No

The main effect of the topic:

The topic requirements are very simple, meaning to construct a binary tree, and then find all the root node to the leaf node path of the sum of numbers, if one is the same as the title, then output Yes, otherwise no

The left parenthesis ' (' indicates a new child node, giving precedence to the left son, and if the left son is established, the right son. The closing parenthesis indicates that the parent node is returned. If a pair of parentheses is empty, it means that the node

It's empty, too.

Ideas for solving problems:

This problem is more difficult to do is the input, with parentheses, there are numbers, there are spaces, but also there will be traps, such as the minus sign and the number may also have spaces between. Then wrote a special input () function is responsible for input, with

This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45533.htm

GetChar () One reads, filters out spaces, and saves them in an array. And then deal with it.

After the binary tree is built, carry on deep search, get the path and.

For empty parentheses, with a simple processing, that is, the node data assigned to a negative infinity, rather than a null pointer, so deep search, if there is a node data value is negative infinity, then do not handle.

#include <iostream> #include <cstdio> #include <cctype> #include <cstring> const int End  
=-214748364;  
    
using namespace Std;  
    Class node{Public:int data;  
    Node *parent;  
    Node *left;  
Node *right;  
    
};  
BOOL found; Node *root, *curroot; Root node pointer node node[10000];  
    
Allocate memory int Nodeindex first;  
int sum;  
    
    
Char str[100000];  
    Creates a two-fork number with only the root node, returns the root node pointer inline node* createbtree () {nodeindex = 1;  
    Node[0].parent = NULL;  
    Node[0].left = NULL;  
    Node[0].right = NULL;  
Return &node[0];  
        } inline node* NewNode (node* parent) {if (nodeindex>10000-2) {node *t = new node;  
        T->left = NULL;  
        T->right = NULL;  
        T->parent = parent;  
    return t;  
    } node[nodeindex].parent = parent;   
    Node[nodeindex].left = Node[nodeindex].right = NULL;  
Return &node[nodeIndex++];  
    } void Solve () {int I, j, ans=0;  
    Node *temp;  
    Curroot = NULL;  
    BOOL flag = FALSE;  
    i=0;  
                while (I<strlen (str)) {//Enter the node and process the data if (str[i]== ' (') {if (Curroot = NULL) {  
            root = Curroot = Createbtree (); } else{if (curroot->left = = NULL) {curroot->left = NewNode (cu Rroot);  Open up space for the left son curroot = curroot->left; Enter the left son} else{curroot->right = NewNode (curroot); Father Curroot = curroot->right;  
            Into the right son}//directly hit the right bracket, indicating that there is no number, this node is a terminal sign ++i;  
                if (str[i]== ') ') {curroot->data = end;  
                Node *parent = curroot->parent;                
            I.;  
            //If the number else{int val;    SSCANF (&str[i], "%d", &val);      
            Curroot->data = val;  
        } else if (str[i] = = ') {curroot = curroot->parent;  
    } ++i;  
    BOOL Input () {char ch;  
    int cnt1=0, cnt2=0, index=0;  
        while (ch = getchar ()) {if (ch== ') ++cnt1;  
        if (ch== ') ') ++cnt2;  
        if (ch== ' | | | ch== ') ' | | | ch== '-' | | isdigit (CH)) str[index++] = ch;  
    if (cnt1 && cnt1==cnt2) break;   
    } getchar ();  
    Str[index] = ' the '; if (cnt1==1 && cnt2==1) return false;  
If you have only a pair of parentheses, you can directly determine return true;  
    } void Dfs (Node *root, int n) {if (found) return; if (root->left->data==end && root->right->data==end) {if (n+root->data = sum) found = Tru  
        E  
    return;  
    } if (Root->left && root->left->data!=end) Dfs (Root->left, n+root->data);if (root->right && root->right->data!=end) Dfs (root->right, n+root->data);  
    int main () {freopen ("Input.txt", "R", stdin); while (~SCANF ("%d", &sum)) {if (!  
        Input ()) {printf ("no\n");  
            } else{Solve ();  
            Found = false;  
            DFS (root, 0);  
            if (found) printf ("yes\n");  
        else printf ("no\n");  
} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.