从给定的数组以级别顺序方式构造一个完整的二叉树
给定一个元素数组,我们的任务是从这个数组以级别顺序的方式构造一个完整的二叉树。也就是说,数组左侧的元素将从级别 0 开始按树级别填充。
例子:
Input : arr[] = {1, 2, 3, 4, 5, 6}
Output : Root of the following tree
1
/ \
2 3
/ \ /
4 5 6
Input: arr[] = {1, 2, 3, 4, 5, 6, 6, 6, 6, 6}
Output: Root of the following tree
1
/ \
2 3
/ \ / \
4 5 6 6
/ \ /
6 6 6
如果我们仔细观察,我们可以看到如果父节点在数组中的索引 i 处,则该节点的左子节点在索引 (2*i + 1) 处,而右子节点在数组中的索引 (2*i + 2) 处大批。
使用这个概念,我们可以通过选择其父节点来轻松插入左右节点。我们将插入数组中存在的第一个元素作为树中第 0 层的根节点,并开始遍历数组,对于每个节点 i,我们将在树中左右插入其子节点。
下面是执行此操作的递归程序:
C++
// CPP program to construct binary
// tree from given array in level
// order fashion Tree Node
#include
using namespace std;
/* A binary tree node has data,
pointer to left child and a
pointer to right child */
struct Node
{
int data;
Node* left, * right;
};
/* Helper function that allocates a
new node */
Node* newNode(int data)
{
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->left = node->right = NULL;
return (node);
}
// Function to insert nodes in level order
Node* insertLevelOrder(int arr[], Node* root,
int i, int n)
{
// Base case for recursion
if (i < n)
{
Node* temp = newNode(arr[i]);
root = temp;
// insert left child
root->left = insertLevelOrder(arr,
root->left, 2 * i + 1, n);
// insert right child
root->right = insertLevelOrder(arr,
root->right, 2 * i + 2, n);
}
return root;
}
// Function to print tree nodes in
// InOrder fashion
void inOrder(Node* root)
{
if (root != NULL)
{
inOrder(root->left);
cout << root->data <<" ";
inOrder(root->right);
}
}
// Driver program to test above function
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 6, 6, 6 };
int n = sizeof(arr)/sizeof(arr[0]);
Node* root = insertLevelOrder(arr, root, 0, n);
inOrder(root);
}
// This code is contributed by Chhavi
Java
// Java program to construct binary tree from
// given array in level order fashion
public class Tree {
Node root;
// Tree Node
static class Node {
int data;
Node left, right;
Node(int data)
{
this.data = data;
this.left = null;
this.right = null;
}
}
// Function to insert nodes in level order
public Node insertLevelOrder(int[] arr, Node root,
int i)
{
// Base case for recursion
if (i < arr.length) {
Node temp = new Node(arr[i]);
root = temp;
// insert left child
root.left = insertLevelOrder(arr, root.left,
2 * i + 1);
// insert right child
root.right = insertLevelOrder(arr, root.right,
2 * i + 2);
}
return root;
}
// Function to print tree nodes in InOrder fashion
public void inOrder(Node root)
{
if (root != null) {
inOrder(root.left);
System.out.print(root.data + " ");
inOrder(root.right);
}
}
// Driver program to test above function
public static void main(String args[])
{
Tree t2 = new Tree();
int arr[] = { 1, 2, 3, 4, 5, 6, 6, 6, 6 };
t2.root = t2.insertLevelOrder(arr, t2.root, 0);
t2.inOrder(t2.root);
}
}
Python3
# Python3 program to construct binary
# tree from given array in level
# order fashion Tree Node
# Helper function that allocates a
#new node
class newNode:
def __init__(self, data):
self.data = data
self.left = self.right = None
# Function to insert nodes in level order
def insertLevelOrder(arr, root, i, n):
# Base case for recursion
if i < n:
temp = newNode(arr[i])
root = temp
# insert left child
root.left = insertLevelOrder(arr, root.left,
2 * i + 1, n)
# insert right child
root.right = insertLevelOrder(arr, root.right,
2 * i + 2, n)
return root
# Function to print tree nodes in
# InOrder fashion
def inOrder(root):
if root != None:
inOrder(root.left)
print(root.data,end=" ")
inOrder(root.right)
# Driver Code
if __name__ == '__main__':
arr = [1, 2, 3, 4, 5, 6, 6, 6, 6]
n = len(arr)
root = None
root = insertLevelOrder(arr, root, 0, n)
inOrder(root)
# This code is contributed by PranchalK
C#
// C# program to construct binary tree from
// given array in level order fashion
using System;
public class Tree
{
Node root;
// Tree Node
public class Node
{
public int data;
public Node left, right;
public Node(int data)
{
this.data = data;
this.left = null;
this.right = null;
}
}
// Function to insert nodes in level order
public Node insertLevelOrder(int[] arr,
Node root, int i)
{
// Base case for recursion
if (i < arr.Length)
{
Node temp = new Node(arr[i]);
root = temp;
// insert left child
root.left = insertLevelOrder(arr,
root.left, 2 * i + 1);
// insert right child
root.right = insertLevelOrder(arr,
root.right, 2 * i + 2);
}
return root;
}
// Function to print tree
// nodes in InOrder fashion
public void inOrder(Node root)
{
if (root != null)
{
inOrder(root.left);
Console.Write(root.data + " ");
inOrder(root.right);
}
}
// Driver code
public static void Main(String []args)
{
Tree t2 = new Tree();
int []arr = { 1, 2, 3, 4, 5, 6, 6, 6, 6 };
t2.root = t2.insertLevelOrder(arr, t2.root, 0);
t2.inOrder(t2.root);
}
}
// This code is contributed Rajput-Ji
Javascript
输出:
6 4 6 2 5 1 6 3 6
时间复杂度:O(n),其中 n 是树中节点的总数。