Path of a binary tree and a value

Source: Internet
Author: User

Question
[Html]
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 22
10 2 3
5 4 5
12-1-1
4-1-1
7-1-1
1 5
1-1-1
Sample output:
Result:
A path is found: 1 2 5
A path is found: 1 3
Result:

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 22
10 2 3
5 4 5
12-1-1
4-1-1
7-1-1
1 5
1-1-1
Sample output:
Result:
A path is found: 1 2 5
A path is found: 1 3
Result:


Ideas
The deformation of the simple tree's forward traversal, you can use the stack to record the size of each node, enough for unique_sum printing.


AC code
[Cpp]
# Include <stdio. h>
# Include <stdlib. h>

Struct btree
{
Int value;
Int loc, lchild, rchild;
};

Struct stack
{
Int top;
Struct btree data [10005];
};

Struct stack * s;

Void print_uniquesum_path (struct btree * bt, int root, int unique)
{
Int lchild, rchild, I, sum;

If (root =-1 ){
Return;
}

S-> data [s-> top ++] = bt [root];

Lchild = bt [root]. lchild;
Rchild = bt [root]. rchild;


If (lchild =-1 & rchild =-1 ){
For (I = sum = 0; I <s-> top; I ++ ){
Sum + = s-> data [I]. value;
}
If (sum = unique ){
Printf ("A path is found :");
For (I = 0; I <s-> top; I ++ ){
Printf ("% d", s-> data [I]. loc );
}
Printf ("\ n ");
}
}

If (lchild <rchild ){
Print_uniquesum_path (bt, lchild, unique );
Print_uniquesum_path (bt, rchild, unique );
} Else {
Print_uniquesum_path (bt, rchild, unique );
Print_uniquesum_path (bt, lchild, unique );
}

S-> top --;
}

Int main ()
{
Int I, n, k;
Struct btree * bt;

While (scanf ("% d", & n, & k )! = EOF ){
// Initialization tree
Bt = (struct btree *) malloc (sizeof (struct btree) * (n + 1 ));
For (I = 1; I <= n; I ++ ){
Scanf ("% d", & bt [I]. value, & bt [I]. lchild, & bt [I]. rchild );
Bt [I]. loc = I;
}

// Initialize the stack
S = (struct stack *) malloc (sizeof (struct stack ));
S-> top = 0;

// Traverse the tree
Printf ("result: \ n ");
Print_uniquesum_path (bt, 1, k );
Free (bt );
}

Return 0;
}
/*************************************** ***********************
Problem: 1368
User: wangzhengyi
Language: C
Result: Accepted
MS Time: 30
Memory: 4112 kb
**************************************** ************************/

# Include <stdio. h>
# Include <stdlib. h>
 
Struct btree
{
Int value;
Int loc, lchild, rchild;
};
 
Struct stack
{
Int top;
Struct btree data [10005];
};
 
Struct stack * s;
 
Void print_uniquesum_path (struct btree * bt, int root, int unique)
{
Int lchild, rchild, I, sum;
 
If (root =-1 ){
Return;
}
 
S-> data [s-> top ++] = bt [root];
 
Lchild = bt [root]. lchild;
Rchild = bt [root]. rchild;
 
 
If (lchild =-1 & rchild =-1 ){
For (I = sum = 0; I <s-> top; I ++ ){
Sum + = s-> data [I]. value;
}
If (sum = unique ){
Printf ("A path is found :");
For (I = 0; I <s-> top; I ++ ){
Printf ("% d", s-> data [I]. loc );
}
Printf ("\ n ");
}
}
 
If (lchild <rchild ){
Print_uniquesum_path (bt, lchild, unique );
Print_uniquesum_path (bt, rchild, unique );
} Else {
Print_uniquesum_path (bt, rchild, unique );
Print_uniquesum_path (bt, lchild, unique );
}
 
S-> top --;
}
 
Int main ()
{
Int I, n, k;
Struct btree * bt;
 
While (scanf ("% d", & n, & k )! = EOF ){
// Initialization tree
Bt = (struct btree *) malloc (sizeof (struct btree) * (n + 1 ));
For (I = 1; I <= n; I ++ ){
Scanf ("% d", & bt [I]. value, & bt [I]. lchild, & bt [I]. rchild );
Bt [I]. loc = I;
}
 
// Initialize the stack
S = (struct stack *) malloc (sizeof (struct stack ));
S-> top = 0;
 
// Traverse the tree
Printf ("result: \ n ");
Print_uniquesum_path (bt, 1, k );
Free (bt );
}
 
Return 0;
}
/*************************************** ***********************
Problem: 1368
User: wangzhengyi
Language: C
Result: Accepted
MS Time: 30
Memory: 4112 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.