The sword refers to the path between the binary tree of the OFFER and a specific value (9 degrees OJ1368)

Source: Internet
Author: User

Description:

Enter a binary tree and an integer to print the sum of the node values in the binary tree and all the paths of the input integers. The path is defined as a path formed from the root node of the tree down to the node through which the leaf node passes.

 

Input:

Each test case contains n + 1 rows:

The first behavior is two integers, n, k (1 <= n <= 10000). n indicates the number of nodes, k indicates the required path and node number from 1 to n.

Next there are n rows. Each row in n contains three integers: vi, leftnode, rightnode, and vi, indicating the value of the I node. leftnode indicates the number of the left child node of the I node, rightnode indicates the number of the right child node of node I. If no knots exist, the value is-1. The node numbered 1 is the root node.

 

Output:

For each test case, "result:" Is output to take one row, and then all paths meeting the conditions are output in alphabetical order. These paths are composed of node numbers. The output format is shown in the output sample.

 

Sample input:
5 2210 2 35 4 512 -1 -14 -1 -17 -1 -11 51 -1 -1

 

Sample output:
result:A path is found: 1 2 5A path is found: 1 3result:
Solution:

First, pay attention to the two requirements of the question,

  1 path: From the root to the leaves, the sum of all nodes is the second number of input nodes.

2. In the Lexicographic Order, that is, 1 is in front of 2. For example, if two paths are 124 13, the output must be in the order of 124 13 rather than 124.

Questions:

  1. When constructing a tree, make sure that the left and right subtree is larger than the right subtree, so that we can scan the left subtree first during scanning, ensure output in dictionary order.

for(i=1;i<=n;i++){            int l,r;            scanf("%d %d %d",&a->arr[i].num, &l, &r);            if(l<r){                a->arr[i].lchild = l;                a->arr[i].rchild = r;            }else{                a->arr[i].lchild = r;                a->arr[i].rchild = l;            }        }

 

  2. It is very easy to use the stack to store the read node elements. Here we have a skill. In the function, because the form parameter is used, the sub-function will not function this form parameter. Therefore, consider using the k parameter to mark the layer of the tree, and use top as the top of the stack.

3. We traverse from the root. If the final leaf meets the condition, the output is:

if(test_sum == sum && a->arr[id].lchild==-1 && a->arr[id].rchild==-1 ){        printf("A path is found:");        int i;        for(i=0;i<top;i++)            printf(" %d", route[i] );        printf("\n");    }
All code:
#include <stdio.h>#include <stdlib.h>#include <memory.h>#define MAXSIZE 10005typedef struct treenode{    int num;    int lchild;    int rchild;}Tree;typedef struct treearr{    struct treenode arr[MAXSIZE];}treeArr; int route[MAXSIZE]={0};int top=0; void traceTree(treeArr *a,int id,int sum,int sum_tmp,int n); int main(){    int n,i,sum;    while(scanf("%d %d",&n,&sum)!=EOF){        treeArr *a = (treeArr *)malloc(sizeof(treeArr));                 for(i=1;i<=n;i++){            int l,r;            scanf("%d %d %d",&a->arr[i].num, &l, &r);            if(l<r){                a->arr[i].lchild = l;                a->arr[i].rchild = r;            }else{                a->arr[i].lchild = r;                a->arr[i].rchild = l;            }        }        printf("result:\n");        memset(&route,0,sizeof(int)*MAXSIZE);        top = 0;        traceTree(a,1,sum,0,1);    }    return 0;}void traceTree(treeArr *a,int id,int sum,int sum_tmp,int k){    if(id == -1 || sum_tmp+a->arr[id].num > sum)        return ;    int test_sum = sum_tmp+a->arr[id].num;    route[top++] = id;    if(test_sum == sum && a->arr[id].lchild==-1 && a->arr[id].rchild==-1 ){        printf("A path is found:");        int i;        for(i=0;i<top;i++)            printf(" %d", route[i] );        printf("\n");    }    traceTree(a,a->arr[id].lchild,sum,test_sum,k+1);    top = k;     traceTree(a,a->arr[id].rchild,sum,test_sum,k+1); }/**************************************************************    Problem: 1368    User: xhalo    Language: C    Result: Accepted    Time:40 ms    Memory:3296 kb****************************************************************/

 

Related Article

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.