Given a binary tree, our task is to print the nodes whose height is a prime number starting from the root node.
Examples:
Input:
1
/ \
2 3
/ \
4 5
Output: 4 5
Explanation:
For this tree:
Height of Node 1 - 0,
Height of Node 2 - 1,
Height of Node 3 - 1,
Height of Node 4 - 2,
Height of Node 5 - 2.
Hence, the nodes whose height
is a prime number are 4, and 5.
Input:
1
/ \
2 5
/ \
3 4
Output: 3 4
Explanation:
For this tree:
Height of Node 1 - 0,
Height of Node 2 - 1,
Height of Node 3 - 2,
Height of Node 4 - 2,
Height of Node 5 - 1.
Hence, the nodes whose height
is a prime number are 3, and 4.
Approach: To solve the problem mentioned above,
- We have to perform Depth First Search(DFS) on the tree and for every node, store the height of every node as we move down the tree.
- Iterate over the height array of each node and check if it prime or not.
- If yes then print the node else ignore it.
Below is the implementation of the above approach:
// C++ implementation of nodes // at prime height in the given tree #include <bits/stdc++.h> using namespace std; #define MAX 100000 vector<int> graph[MAX + 1]; // To store Prime Numbers vector<bool> Prime(MAX + 1, true); // To store height of each node int height[MAX + 1]; // Function to find the // prime numbers till 10^5 void SieveOfEratosthenes() { int i, j; Prime[0] = Prime[1] = false; for (i = 2; i * i <= MAX; i++) { // Traverse all multiple of i // and make it false if (Prime[i]) { for (j = 2 * i; j < MAX; j += i) { Prime[j] = false; } } } } // Function to perform dfs void dfs(int node, int parent, int h) { // Store the height of node height[node] = h; for (int to : graph[node]) { if (to == parent) continue; dfs(to, node, h + 1); } } // Function to find the nodes // at prime height void primeHeightNode(int N) { // To precompute prime number till 10^5 SieveOfEratosthenes(); for (int i = 1; i <= N; i++) { // Check if height[node] is prime if (Prime[height[i]]) { cout << i << " "; } } } // Driver code int main() { // Number of nodes int N = 5; // Edges of the tree graph[1].push_back(2); graph[1].push_back(3); graph[2].push_back(4); graph[2].push_back(5); dfs(1, 1, 0); primeHeightNode(N); return 0; } |
4 5
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Relationship between number of nodes and height of binary tree
- Height of a complete binary tree (or Heap) with N nodes
- Find height of a special binary tree whose leaf nodes are connected
- Print middle level of perfect binary tree without finding height
- Print the nodes with a prime degree in given Prufer sequence of a Tree
- Count of nodes in a Binary Tree whose immediate children are co-prime
- Count of nodes in a Binary Tree whose child is its prime factors
- Print nodes in the Top View of Binary Tree | Set 3
- Print all even nodes of Binary Search Tree
- Print all full nodes in a Binary Tree
- Print path between any two nodes in a Binary Tree
- Print the nodes of Binary Tree having a grandchild
- Print path between any two nodes in a Binary Tree | Set 2
- Print Levels of all nodes in a Binary Tree
- Print all nodes in a binary tree having K leaves
- Print Nodes in Top View of Binary Tree
- Print nodes in top view of Binary Tree | Set 2
- Print the nodes having exactly one child in a Binary tree
- Print all internal nodes of a Binary tree
- Print all nodes between two given levels in Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

