Skip to content
Back to libraryPart 4 · Advanced topics

4.1Trees

Before you start

It helps to have read 3.1 Graph search: BFS and DFS and 3.4 Dynamic programming I. Overall level: Advanced.

Practice: Trees

0 of 4 solved

About these problems

Apply, Connect, Reason and Stretch describe difficulty within this topic, not an official contest rating. Open any problem in any order; reading the chapter first is optional.

  1. 1Subtree sizesAn undirected tree has n vertices, numbered 1 to n, and is rooted at vertex 1. Print the number of vertices in each vertex's subtree, including the vertex itself.ApplyNot started
  2. 2Subtree rewardsA tree has n vertices, numbered 1 to n, and is rooted at vertex 1. Vertex i has an integer reward ai. Print the total reward in each vertex's subtree, including the vertex itself.ConnectNot started
  3. 3Tree diameterAn unweighted tree has n vertices, numbered 1 to n. Find the greatest distance between two vertices, where distance is the number of edges on the path between them.ReasonNot started
  4. 4Farthest from each vertexAn unweighted tree has n vertices, numbered 1 to n. Print, for every vertex, its greatest distance to any vertex of the tree, measured in edges.StretchNot started

Read the lesson