Print the nodes of the Binary Tree whose height is a Prime number
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++
#include <bits/stdc++.h>
using namespace std;
#define MAX 100000
vector< int > graph[MAX + 1];
vector< bool > Prime(MAX + 1, true );
int height[MAX + 1];
void SieveOfEratosthenes()
{
int i, j;
Prime[0] = Prime[1] = false ;
for (i = 2; i * i <= MAX; i++) {
if (Prime[i]) {
for (j = 2 * i; j < MAX; j += i) {
Prime[j] = false ;
}
}
}
}
void dfs( int node, int parent, int h)
{
height[node] = h;
for ( int to : graph[node]) {
if (to == parent)
continue ;
dfs(to, node, h + 1);
}
}
void primeHeightNode( int N)
{
SieveOfEratosthenes();
for ( int i = 1; i <= N; i++) {
if (Prime[height[i]]) {
cout << i << " " ;
}
}
}
int main()
{
int N = 5;
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;
}
|
Java
import java.util.*;
class GFG{
static final int MAX = 100000 ;
@SuppressWarnings ( "unchecked" )
static Vector<Integer> []graph = new Vector[MAX + 1 ];
static boolean []Prime = new boolean [MAX + 1 ];
static int []height = new int [MAX + 1 ];
static void SieveOfEratosthenes()
{
int i, j;
Prime[ 0 ] = Prime[ 1 ] = false ;
for (i = 2 ; i * i <= MAX; i++)
{
if (Prime[i])
{
for (j = 2 * i; j < MAX; j += i)
{
Prime[j] = false ;
}
}
}
}
static void dfs( int node, int parent, int h)
{
height[node] = h;
for ( int to : graph[node])
{
if (to == parent)
continue ;
dfs(to, node, h + 1 );
}
}
static void primeHeightNode( int N)
{
SieveOfEratosthenes();
for ( int i = 1 ; i <= N; i++)
{
if (Prime[height[i]])
{
System.out.print(i + " " );
}
}
}
public static void main(String[] args)
{
int N = 5 ;
for ( int i = 0 ; i < Prime.length; i++)
Prime[i] = true ;
for ( int i = 0 ; i < graph.length; i++)
graph[i] = new Vector<Integer>();
graph[ 1 ].add( 2 );
graph[ 1 ].add( 3 );
graph[ 2 ].add( 4 );
graph[ 2 ].add( 5 );
dfs( 1 , 1 , 0 );
primeHeightNode(N);
}
}
|
Python3
MAX = 100000
graph = [[] for i in range ( MAX + 1 )]
Prime = [ True for i in range ( MAX + 1 )]
height = [ 0 for i in range ( MAX + 1 )]
def SieveOfEratosthenes():
Prime[ 0 ] = Prime[ 1 ] = False
i = 2
while i * i < = MAX :
if (Prime[i]):
for j in range ( 2 * i, MAX , i):
Prime[j] = False
i + = 1
def dfs(node, parent, h):
height[node] = h
for to in graph[node]:
if (to = = parent):
continue
dfs(to, node, h + 1 )
def primeHeightNode(N):
SieveOfEratosthenes()
for i in range ( 1 , N + 1 ):
if (Prime[height[i]]):
print (i, end = ' ' )
if __name__ = = "__main__" :
N = 5
graph[ 1 ].append( 2 )
graph[ 1 ].append( 3 )
graph[ 2 ].append( 4 )
graph[ 2 ].append( 5 )
dfs( 1 , 1 , 0 )
primeHeightNode(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static readonly int MAX = 100000;
static List< int >[] graph = new List< int >[ MAX + 1 ];
static bool [] Prime = new bool [MAX + 1];
static int [] height = new int [MAX + 1];
static void SieveOfEratosthenes()
{
int i, j;
Prime[0] = Prime[1] = false ;
for (i = 2; i * i <= MAX; i++)
{
if (Prime[i])
{
for (j = 2 * i; j < MAX; j += i)
{
Prime[j] = false ;
}
}
}
}
static void dfs( int node, int parent, int h)
{
height[node] = h;
foreach ( int to in graph[node])
{
if (to == parent)
continue ;
dfs(to, node, h + 1);
}
}
static void primeHeightNode( int N)
{
SieveOfEratosthenes();
for ( int i = 1; i <= N; i++)
{
if (Prime[height[i]])
{
Console.Write(i + " " );
}
}
}
public static void Main(String[] args)
{
int N = 5;
for ( int i = 0; i < Prime.Length; i++)
Prime[i] = true ;
for ( int i = 0; i < graph.Length; i++)
graph[i] = new List< int >();
graph[1].Add(2);
graph[1].Add(3);
graph[2].Add(4);
graph[2].Add(5);
dfs(1, 1, 0);
primeHeightNode(N);
}
}
|
Javascript
<script>
let MAX = 100000;
let graph = []
for (let i = 0; i < MAX + 1; i++){
graph.push([])
}
let Prime = new Array(MAX + 1).fill( true );
let height = new Array(MAX + 1);
function SieveOfEratosthenes()
{
let i, j;
Prime[0] = Prime[1] = false ;
for (i = 2; i * i <= MAX; i++) {
if (Prime[i]) {
for (j = 2 * i; j < MAX; j += i) {
Prime[j] = false ;
}
}
}
}
function dfs(node, parent, h)
{
height[node] = h;
for (let to of graph[node]) {
if (to == parent)
continue ;
dfs(to, node, h + 1);
}
}
function primeHeightNode(N)
{
SieveOfEratosthenes();
for (let i = 1; i <= N; i++) {
if (Prime[height[i]]) {
document.write(i + " " );
}
}
}
let N = 5;
graph[1].push(2);
graph[1].push(3);
graph[2].push(4);
graph[2].push(5);
dfs(1, 1, 0);
primeHeightNode(N);
</script>
|
Time Complexity: O(N+MAX*log(log(MAX)))
Auxiliary Space: O(MAX)
Last Updated :
28 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...