Ouch! A kitten got stuck on a tree. Fortunately, the tree's branches are numbered. Given a description of a tree and the position of the kitten, can you write a program to help the kitten down? Input Kitten on a Tree 14 25 24 4312 13 9 4 11 10 20 8 7 32 10 21 23 13 19 32 22 19 12 5 14 17 30 14 6 15 16 30 18 31 29 24 23 26 26 27 28 -1 20 The input is a description of a single tree. The first line contains an integer K , denoting the branch on which the kitten got stuck. The next lines each contain two or more integers a, b₁,b2,.... Each such line denotes a branching: the kitten can reach a from b₁,b2,... on its way down. Thus, a will be closer to the root than any of the bi. The description ends with a line containing -1. Each branch b; will appear on exactly one line. All branch numbers are in the range 1..100, though not necessarily contiguous. You are guaranteed that there is a path from every listed branch to the root. The kitten will sit on a branch that has a number that is different than the root. The illustration above corresponds to the sample input. Output Output the path to the ground, starting with the branch on which the kitten sits. Sample Input 1 Sample Output 1 23 14 19 23 24 25 19

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question

Code in PYTHON

Ouch! A kitten got stuck on a tree.
Fortunately, the tree's branches are
numbered. Given a description of a tree
and the position of the kitten, can you
write a program to help the kitten down?
Input
The input is a description of a single
tree. The first line contains an integer K
, denoting the branch on which the
kitten got stuck. The next lines each
contain two or more integers
Kitten on a Tree
24
14
25 24
4 3 1 2
13 9 4 11
10 20 8 7
32 10 21
23 13 19 32 22
19 12 5 14 17 30
14 6 15 16
30 18 31 29
24 23 26
32
26 27 28
-1
23
26
14 19 23 24 25
19
a, b₁,b2,.... Each such line denotes a branching: the kitten can reach a from
b₁,b2,... on its way down. Thus, a will be closer to the root than any of the b;. The
description ends with a line containing -1. Each branch b; will appear on exactly
one line. All branch numbers are in the range 1..100, though not necessarily
contiguous. You are guaranteed that there is a path from every listed branch to
the root. The kitten will sit on a branch that has a number that is different than
the root.
The illustration above corresponds to the sample input.
16
Output
Output the path to the ground, starting with the branch on which the kitten sits.
Sample Input 1
Sample Output 1
15
18
<Hide
0
Transcribed Image Text:Ouch! A kitten got stuck on a tree. Fortunately, the tree's branches are numbered. Given a description of a tree and the position of the kitten, can you write a program to help the kitten down? Input The input is a description of a single tree. The first line contains an integer K , denoting the branch on which the kitten got stuck. The next lines each contain two or more integers Kitten on a Tree 24 14 25 24 4 3 1 2 13 9 4 11 10 20 8 7 32 10 21 23 13 19 32 22 19 12 5 14 17 30 14 6 15 16 30 18 31 29 24 23 26 32 26 27 28 -1 23 26 14 19 23 24 25 19 a, b₁,b2,.... Each such line denotes a branching: the kitten can reach a from b₁,b2,... on its way down. Thus, a will be closer to the root than any of the b;. The description ends with a line containing -1. Each branch b; will appear on exactly one line. All branch numbers are in the range 1..100, though not necessarily contiguous. You are guaranteed that there is a path from every listed branch to the root. The kitten will sit on a branch that has a number that is different than the root. The illustration above corresponds to the sample input. 16 Output Output the path to the ground, starting with the branch on which the kitten sits. Sample Input 1 Sample Output 1 15 18 <Hide 0
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Time complexity
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning