原文:
给定一个作为数组,任务是按排序顺序打印它的所有级别。 例:
input: arr[] = {7, 6, 5, 4, 3, 2, 1}
the given tree looks like
7
/ \
6 5
/ \ / \
4 3 2 1
output:
7
5 6
1 2 3 4
input: arr[] = {5, 6, 4, 9, 2, 1}
the given tree looks like
5
/ \
6 4
/ \ /
9 2 1
output:
5
4 6
1 2 9
方法:这里讨论了一个类似的问题 因为给定的树是一个 :
no. of nodes at a level l will be 2l where l ≥ 0
- 开始遍历级别初始化为 0 的数组。
- 并打印元素。
以下是上述方法的实现:
c
// c implementation of the approach
#include
using namespace std;
// function to print all the levels
// of the given tree in sorted order
void printsortedlevels(int arr[], int n)
{
// initialize level with 0
int level = 0;
for (int i = 0; i < n; level ) {
// number of nodes at current level
int cnt = (int)pow(2, level);
// indexing of array starts from 0
// so subtract no. of nodes by 1
cnt -= 1;
// index of the last node in the current level
int j = min(i cnt, n - 1);
// sort the nodes of the current level
sort(arr i, arr j 1);
// print the sorted nodes
while (i <= j) {
cout << arr[i] << " ";
i ;
}
cout << endl;
}
}
// driver code
int main()
{
int arr[] = { 5, 6, 4, 9, 2, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
printsortedlevels(arr, n);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java implementation of the approach
import java.util.arrays;
class gfg
{
// function to print all the levels
// of the given tree in sorted order
static void printsortedlevels(int arr[], int n)
{
// initialize level with 0
int level = 0;
for (int i = 0; i < n; level )
{
// number of nodes at current level
int cnt = (int)math.pow(2, level);
// indexing of array starts from 0
// so subtract no. of nodes by 1
cnt -= 1;
// index of the last node in the current level
int j = math.min(i cnt, n - 1);
// sort the nodes of the current level
arrays.sort(arr, i, j 1);
// print the sorted nodes
while (i <= j)
{
system.out.print(arr[i] " ");
i ;
}
system.out.println();
}
}
// driver code
public static void main(string[] args)
{
int arr[] = { 5, 6, 4, 9, 2, 1 };
int n = arr.length;
printsortedlevels(arr, n);
}
}
// this code is contributed by 29ajaykumar
python 3
# python3 implementation of the approach
from math import pow
# function to print all the levels
# of the given tree in sorted order
def printsortedlevels(arr, n):
# initialize level with 0
level = 0
i = 0
while(i < n):
# number of nodes at current level
cnt = int(pow(2, level))
# indexing of array starts from 0
# so subtract no. of nodes by 1
cnt -= 1
# index of the last node in the current level
j = min(i cnt, n - 1)
# sort the nodes of the current level
arr = arr[:i] sorted(arr[i:j 1]) \
arr[j 1:]
# print the sorted nodes
while (i <= j):
print(arr[i], end = " ")
i = 1
print()
level = 1
# driver code
arr = [ 5, 6, 4, 9, 2, 1]
n = len(arr)
printsortedlevels(arr, n)
# this code is contributed by shubhamsingh10
c
// c# implementation of the approach
using system;
using system.linq;
class gfg
{
// function to print all the levels
// of the given tree in sorted order
static void printsortedlevels(int []arr, int n)
{
// initialize level with 0
int level = 0;
for (int i = 0; i < n; level )
{
// number of nodes at current level
int cnt = (int)math.pow(2, level);
// indexing of array starts from 0
// so subtract no. of nodes by 1
cnt -= 1;
// index of the last node in the current level
int j = math.min(i cnt, n - 1);
// sort the nodes of the current level
array.sort(arr, i, j 1 - i);
// print the sorted nodes
while (i <= j)
{
console.write(arr[i] " ");
i ;
}
console.writeline();
}
}
// driver code
public static void main(string[] args)
{
int []arr = { 5, 6, 4, 9, 2, 1 };
int n = arr.length;
printsortedlevels(arr, n);
}
}
// this code is contributed by 29ajaykumar
java 描述语言
output:
5
4 6
1 2 9
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处