[Huawei OD test questions] Finding the path (java) 100% pass rate [2023 (B volume) 100 points]

find the path

topic description

A binary tree can also be stored in an array. Given an array, the value of the root node of the tree is stored at subscript 1.
For a node stored at subscript n, its left and right child nodes are stored at subscripts 2 n and 2 n respectively. +1,
and we use -1 to indicate that a node is empty.
Given a binary tree stored in an array, try to find the path from the root node to the smallest leaf node, the path consists of the node's value.

enter description

Enter the contents of an array with one line, each element of the array is a positive integer, and the elements are separated by spaces.
Note that the first element is the value of the root node, that is, the nth element of the array corresponds to the subscript n.
Subscript 0 is not used in the tree representation, so we omit it.
The input tree has up to 7 layers.

output description

Output the value of each node on the path from the root node to the smallest leaf node separated by spaces
Use cases to ensure that there is only one smallest leaf node

example one

enter

3 5 7 -1 -1 2 4

output

 

Guess you like

Origin blog.csdn.net/weixin_45541762/article/details/132136551