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

PROGRAMMING:Binary tree paths and II

Luz5年前 (2021-05-10)题库414
Write a program to find the largest path in the non empty binary tree. The node of the binary tree is an integer not equal to 0. The "path" of this problem is defined as the node sequence in the binary tree_{ i},...,v_{ j} $$, the former node in the sequence is the parent node of the latter node, but the path does not necessarily start with the root node or end with the leaf node. The sum of a path is defined as the sum of the data values of all nodes contained in the path.
###Input format:
The input is a group of integers separated by spaces, the number of which is not more than 100, indicating the first root sequence of binary tree with null pointer information.
###Output format:
The output is two lines, the first line is the maximum value of the binary tree path sum, the second line is a group of integers, and a space after each integer represents the node value contained in the maximum path (output in the order of increasing the number of layers). If there are multiple paths satisfying the conditions, the shortest path (including the least number of nodes) will be output. If there are multiple shortest paths, the shortest path on the left will be output.
###Input sample 1:
```in
1 2 0 0 3 0 0
```
###Output sample 1:
```out
four
1 3
```
###Input sample 2:
```in
-1 2 0 0 3 4 0 0 0
```
###Output sample 2:
```out
seven
3 4
```
###Input sample 3:
```in
3 2 0 0 -1 4 0 0 0
```
###Output sample 3:
```out
six
3 -1 4
```







answer:If there is no answer, please comment