-->
当前位置:首页 > 题库

PROGRAMMING:Binary tree and path equal to a certain value

Luz5年前 (2021-05-10)题库398
It is known that the node of binary tree is an integer not equal to 0. Given an integer k, please write a program to find all paths with root node as the starting point and leaf node as the ending point, and the sum of node values is equal to K. For example, k = 15, for the binary tree T shown in the figure below, there are two paths that meet the conditions, namely 8-5-2 and 8-7. If there is no path satisfying the condition, it can also be identified.
![ img.jpg](~/f1bd43b7-796e-47ae-a9f5-8ffc0300858b.jpg)
###Input format:
The input is 2 lines. The first line is a group of integers separated by spaces. The number of integers is not more than 100. It represents the first root sequence of binary tree with null pointer information. Null pointer information is represented by 0. The second line is the integer K.
###Output format:
The first line of output is an integer representing the number of paths satisfying the condition; If there is no path satisfying the condition, output 0. Starting from the second line, each line has a path that meets the conditions. If there are multiple paths that meet the conditions, they will be output from left to right. There is a space after each node value in the path. If the node values of two different paths are exactly equal, they need to be output.
###Input sample 1:
```in
8 5 1 0 0 2 0 0 7 0 0
fifteen
```
###Output sample 1:
```out
two
8 5 2
8 7
```
###Input sample 2:
```in
-1 2 0 0 3 0 0
two
```
###Output sample 2:
```out
one
-1 3
```
###Input sample 3:
```in
1 1 0 0 1 0 0
two
```
###Output sample 3:
```out
two
1 1
1 1
```
###Input sample 4:
```in
-1 2 0 0 3 0 0
eight
```
###Output sample 4:
```out
0
```







answer:If there is no answer, please comment