给定N和K,请打印一棵树,以使该树不超过K个叶节点,并且每隔一个节点至少有两个与之连接的节点。任务是精确地构建N个节点的树,以使最远叶节点之间的距离最小。同时打印最小距离。
注意:可以有多棵树。
例子:
Input: N = 5, K = 3
Output: Distance = 3
The tree is:
1 2
2 3
3 4
3 5
Input: N = 3, K = 2
Output: Distance = 2
The tree is:
1 2
2 3
方法:
- 最初,树将有k-1个节点连接到1。
- 然后将一个节点一个接一个地连接到所有k-1个节点。
- 如果剩下节点,请保持将它们一一连接到叶节点。
如何构建树的图形表示将使事情变得更加清晰。在下面的图片中,K = 6,并且对于任何数量的N都得到了证明。黄色的节点是叶节点。
下面是上述方法的实现:
C++
// C++ program of above approach
#include
using namespace std;
// Function to print the distance
// and the tree
void buildTree(int n, int k)
{
int ans = 2 * ((n - 1) / k) + min((n - 1) % k, 2);
cout << "Distance = " << ans;
cout << "\nThe tree is:\n";
// print all K-1 leaf nodes attached with 1
for (int i = 2; i <= k; i++) {
cout << "1 " << i << endl;
}
// Join nodes to from other left nodes
// the last node thus will be the left out leaf node
for (int i = k + 1; i <= n; i++) {
cout << i << " " << (i - k) << endl;
}
}
// Driver Code
int main()
{
int n = 5, k = 3;
buildTree(n, k);
}
Java
// Java program of above approach
import java.util.*;
import java.lang.*;
// Function to print the distance
// and the tree
class GFG
{
public void buildTree(int n, int k)
{
int ans = 2 * ((n - 1) / k) +
Math.min((n - 1) % k, 2);
System.out.println("Distance = " + ans);
System.out.println("The tree is: ");
// print all K-1 leaf nodes
// attached with 1
for (int i = 2; i <= k; i++)
{
System.out.println( "1 " + i );
}
// Join nodes to from other left
// nodes the last node thus will
// be the left out leaf node
for (int i = k + 1; i <= n; i++)
{
System.out.println( i + " " +
(i - k));
}
}
// Driver Code
public static void main(String args[])
{
GFG g = new GFG();
int n = 5, k = 3;
g.buildTree(n, k);
}
}
// This code is contributed
// by Akanksha Rai(Abby_akku)
Python3
# Python3 program of above approach
# Function to print the distance
# and the tree
def buildTree(n, k):
ans = (2 * ((n - 1) // k) +
min((n - 1) % k, 2))
print("Distance = ", ans )
print("The tree is:")
# print all K-1 leaf nodes
# attached with 1
for i in range(2, k + 1):
print("1 ", i)
# Join nodes to from other left nodes
# the last node thus will be the
# left out leaf node
for i in range(k + 1, n + 1):
print(i, "", (i - k))
# Driver Code
if __name__ == '__main__':
n = 5
k = 3
buildTree(n, k)
# This code is contributed
# by SHUBHAMSINGH10
C#
// C# program of above approach
using System;
// Function to print the distance
// and the tree
class GFG
{
public void buildTree(int n, int k)
{
int ans = 2 * ((n - 1) / k) +
Math.Min((n - 1) % k, 2);
Console.WriteLine("Distance = " + ans);
Console.WriteLine ("The tree is: ");
// print all K-1 leaf nodes
// attached with 1
for (int i = 2; i <= k; i++)
{
Console.WriteLine( "1 " + i );
}
// Join nodes to from other left
// nodes the last node thus will
// be the left out leaf node
for (int i = k + 1; i <= n; i++)
{
Console.WriteLine ( i + " " +
(i - k));
}
}
// Driver Code
public static void Main()
{
GFG g = new GFG();
int n = 5, k = 3;
g.buildTree(n, k);
}
}
// This code is contributed by Soumik
PHP
Javascript
输出:
Distance = 3
The tree is:
1 2
1 3
4 1
5 2
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。