Bountiful Tree
InterviewTime limit1sMemory limit1024 MB
Given a tree, decide whether every node has degree 1 or 3 and all leaves are equally far from the root, then list all valid roots.
- Level
Medium6 of 10
- Topics
- Tree, DFS, Implementation
- Solved
- No attempts yet
Problem
Rumae likes every kind of tree, but one kind is her favorite. She calls a tree that meets the following conditions a 'bountiful tree'.
- Every node has degree or . The root node has degree .
- The distance from the root node to every node with degree is the same.
The distance between two nodes is the number of edges in the simple path connecting them. Curious Rumae wants to know whether a given tree is a 'bountiful tree'. Rumae also likes to sit at the top of a bountiful tree, so if the given tree is a 'bountiful tree', she wants to know which nodes can be the root.
Input
The first line contains the number of nodes of the tree. ()
The next lines each contain two nodes and , separated by a space. () Node and node are connected by an edge.
The input is guaranteed to be a tree.
Output
If the given tree can be a 'bountiful tree', print on the first line the number of nodes that can be the root. On the second line, print all such nodes in ascending order, separated by spaces.
If the given tree cannot be a 'bountiful tree', print .