Skip to main content
All Questions

Return the minimum set of nodes to delete so a rooted tree has height at most k.

Unlock detailed company stats for this questionUpgrade

Given a graph of height >= k rooted at node = 0, return the minimum set of nodes to delete (not the count) such that the resulting tree has height at most k. Deleting a node removes it from consideration in the height count, but everything in its subtree is still included. Follow-up variant: besides minimizing the number of deleted nodes, also maximize the score of the deleted nodes (minimize the sum of their heights, prioritizing nodes lower in the tree). Test cases for the follow-up variant: n=5, edges=[[0,1],[1,2],[2,3],[3,4]], k=3 gives min size 2, min score 3 (nodes 3 and 4); n=7, edges=[[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], k=2 gives min size 1, min score 3 (node 0); n=7, edges=[[0,1],[0,2],[0,3],[3,4],[4,5],[5,6]], k=2 gives min size 3, min score 6 (nodes 4, 5, 6); n=6, edges=[[0,1],[1,2],[2,3],[0,4],[4,5]], k=3 gives min size 1, min score 2 (node 3).

Expect follow-ups like

  • Among minimum-size deletion sets, also maximize the score of the deleted nodes, essentially minimizing the sum of their heights and prioritizing nodes lower in the tree.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.