# Path of a binary tree and a value

Source: Internet
Author: User
Question
` 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 an integer of N, K (1 <= n <= 10000), and 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 occupy one row, and then all paths meeting the conditions are output in alphabetical order. These paths are composed of node numbers, for the output format, see 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 3 result: `
A simple idea is the transformation of the tree's forward traversal. You can use the stack to record the size of each node. If unique_sum is enough, you can print the AC. Code
`# 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) {f Or (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 stack S = (struct stack *) malloc (sizeof (struct stack); s-> Top = 0; // print the tree printf ("Result: \ n"); print_uniquesum_path (BT, 1, k); free (BT) ;}return 0 ;} /*************************************** * *********************** problem: 1368 User: wangzhengyi language: C result: accepted time: 30 MS memory: 4112 kb ************************************** **************************/`

Related Keywords:

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.