VOOZH about

URL: https://www.geeksforgeeks.org/dsa/cses-solutions-prufer-code/

⇱ CSES Solutions - Prüfer Code - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CSES Solutions - Prüfer Code

Last Updated : 23 Jul, 2025

A Prüfer code of a tree of n nodes is a sequence of n-2 integers that uniquely specifies the structure of the tree. The code is constructed as follows: As long as there are at least three nodes left, find a leaf with the smallest label, add the label of its only neighbor to the code, and remove the leaf from the tree. Given a Prüfer code of a tree, your task is to construct the original tree .

Examples:

Input : n = 5, prufer code = 2 2 4
Output:
1 2
2 3
2 4
4 5
Explanation : The Prüfer code {2, 2, 4} builds a tree with leaves connecting to node 2 (1-2, 2-3, 2-4), and the remaining node (4) connects to the last leaf (4-5) for a total of 4 edges.

Input : n = 6, prufer code = 1 3 3 1
Output : Edges : 2-1 , 4 -3, 5-3, 3 -1, 1- 6
Explanation : The Prüfer code {1, 3, 3, 1} reveals connections for leaves (2-1, 4-3, 5-3), reconstructing the tree with remaining nodes (3-1, 1-6) for a total of 5 edges.

Approach: To solve the problem, follow the below idea:

Prufer Code provides a unique code for every tree. Each tree can have only one Prufer code and each Prufer Code can map to exactly one Tree.

Imagine you have a secret code that tells you how to build a tree, but it only mentions connections for leafy branches (ones with just one connection). That's kind of how a Prüfer code works.

While generating the Prufer Code, we always add the neighbor of the leaf node to the Prufer Code. So, we know that all the nodes, which are not present in the Prufer Code are leaves of the tree. So, now we can start constructing out tree from the leaf nodes.

  • Degree of all nodes in the Prufer Code: All the nodes which are not present in the Prufer Code are leaf nodes and have degree = 0.
  • Start from the leaves: Start from the leaf node, and add an edge between first node of the Prufer Code and the leaf node.
  • Decrease the degree of the nodes: Decrease the degree of the first node of the Prufer Code and the leaf node. If the degree of nodes become less than 0, then it means that those nodes will no longer form any edge with any node. If the degree of node becomes 0, then it means we can consider it as a leaf node to form edges with another node.
  • Move to the next node in the Prufer Code: Move to the next node in the Prufer Code and make an edge with this node and the smallest node which has indegree of 0.
  • Repeat for the remaining nodes: Repeat the above step till all the nodes of the Prufer Code are finished.
  • Last Two Nodes: After all the nodes of Prufer Code are covered, then there will be 2 nodes left with degree = 0. Add an edge between those two nodes to construct the final graph.

Step-by-step algorithm:

  • Maintain a set, say availableNodes to store the potential leaves of the tree.
  • Push all the nodes in the availableNodes.
  • Remove the nodes which are in the PruferCode from availableNodes as they can never be leaf nodes.
  • Iterate over all the elements of PruferCode,
    • For any node at index i, PruferCode[i] find the smallest leaf node from availableNodes and print the edge between PruferCode[i] and smallest leaf.
    • Remove the smallest leaf from the availableNodes.
    • Decrease the degree of node PruferCode[i] and push it in availableNodes if its degree becomes 0.
  • Get the remaining two nodes from the set availableNodes.
  • Print the last edge connecting these two nodes.

Below is the implementation of the above algorithm:


Output
2 1
4 3
5 3
3 1
1 6

Time Complexity: O(N * logN), where N is the number of nodes in the graph.
Auxiliary Space: O(N)

Comment
Article Tags:
Article Tags: