原文:

给定一棵二叉树,我们的任务是从根节点开始打印高度为的节点。

*例:*

**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.

*方法:*为了解决上面提到的问题,

we must perform on the tree, and for each node, store the of each node when we move down. iterate the height array of each node and check whether it is prime .* if yes, print the node; otherwise, ignore it.

下面是上述方法的实现:

c

// c   implementation of nodes
// at prime height in the given tree
#include 
using namespace std;
#define max 100000
vector graph[max   1];
// to store prime numbers
vector 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;
}

java 语言(一种计算机语言,尤用于创建网站)

// java implementation of nodes
// at prime height in the given tree
import java.util.*;
class gfg{
static final int max = 100000;
@suppresswarnings("unchecked")
static vector []graph = new vector[max   1];
// to store prime numbers
static boolean []prime = new boolean[max   1];
// to store height of each node
static int []height = new int[max   1];
// function to find the
// prime numbers till 10^5
static 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
static 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
static 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]])
        {
            system.out.print(i   " ");
        }
    }
}
// driver code
public static void main(string[] args)
{
    // number of nodes
    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();
    // edges of the tree
    graph[1].add(2);
    graph[1].add(3);
    graph[2].add(4);
    graph[2].add(5);
    dfs(1, 1, 0);
    primeheightnode(n);
}
}
// this code is contributed by 29ajaykumar

python 3

# python3 implementation of nodes
# at prime height in the given tree
max = 100000
graph = [[] for i in range(max   1)]
# to store prime numbers
prime = [true for i in range(max   1)]
# to store height of each node
height = [0 for i in range(max   1)]
# function to find the
# prime numbers till 10^5
def sieveoferatosthenes():
    prime[0] = prime[1] = false
    i = 2
    while i * i <= max:
        # traverse all multiple of i
        # and make it false
        if (prime[i]):
            for j in range(2 * i, max, i):
                prime[j] = false
        i  = 1
# function to perform dfs
def dfs(node, parent, h):
    # store the height of node
    height[node] = h
    for to in  graph[node]:
        if (to == parent):
            continue
        dfs(to, node, h   1)
# function to find the nodes
# at prime height
def primeheightnode(n):
    # to precompute prime
    # number till 10^5
    sieveoferatosthenes()
    for i in range(1, n   1):
        # check if height[node] is prime
        if (prime[height[i]]):
            print(i, end = ' ')
# driver code
if __name__=="__main__":
    # number of nodes
    n = 5
    # edges of the tree
    graph[1].append(2)
    graph[1].append(3)
    graph[2].append(4)
    graph[2].append(5)
    dfs(1, 1, 0)
    primeheightnode(n)
# this code is contributed by rutvik_56

c#

// c# implementation of nodes
// at prime height in the given tree
using system;
using system.collections.generic;
class gfg{
    static readonly int max = 100000;
    static list[] graph = new list[ max   1 ];
    // to store prime numbers
    static bool[] prime = new bool[max   1];
    // to store height of each node
    static int[] height = new int[max   1];
    // function to find the
    // prime numbers till 10^5
    static 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
    static void dfs(int node, int parent, int h)
    {
        // store the height of node
        height[node] = h;
        foreach(int to in graph[node])
        {
            if (to == parent)
                continue;
            dfs(to, node, h   1);
        }
    }
    // function to find the nodes
    // at prime height
    static 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]])
            {
                console.write(i   " ");
            }
        }
    }
    // driver code
    public static void main(string[] args)
    {
        // number of nodes
        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();
        // edges of the tree
        graph[1].add(2);
        graph[1].add(3);
        graph[2].add(4);
        graph[2].add(5);
        dfs(1, 1, 0);
        primeheightnode(n);
    }
}
// this code is contributed by amit katiyar

java 描述语言


**output: 

4 5**

时间复杂度:0(n)

辅助空间:0(最大)