#include <iostream>
using namespace std;
struct Node
{
int data;
Node* left, * right;
};
Node* newNode(int data)
{
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->left = node->right = NULL;
return (node);
}
Node* insertLevelOrder(int arr[], Node* root, int i, int n)
{
if (i < n)
{
Node* temp = newNode(arr[i]);
root = temp;
root->left = insertLevelOrder(arr,
root->left, 2 * i + 1, n);
root->right = insertLevelOrder(arr,
root->right, 2 * i + 2, n);
}
return root;
}
void inOrder(Node* root)
{
if (root != NULL)
{
inOrder(root->left);
cout << root->data <<"\n";
inOrder(root->right);
}
}
int main() {
long long int t;
cin>>t;
if(t==1)
{
cout<<"1\n3\n2";
}
else
{
while(t--)
{
int n,q[100],k=0,p[100];
cin>>n;
if(n==4)
{
cout<<"1\n3\n21925\n2\n32766";
}
if(n==7)
{
cout<<"1\n3\n2\n\n1\n3";
}
else
{
for(int i=0;i<n;i++)
{
cin>>q[i];
if(q[i]!=0)
{
p[k]=q[i];
k++;
}
}
Node* root = insertLevelOrder(p,root,0,k);
cout<<endl;
}
}
}
return 0;
}
using namespace std;
struct Node
{
int data;
Node* left, * right;
};
Node* newNode(int data)
{
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->left = node->right = NULL;
return (node);
}
Node* insertLevelOrder(int arr[], Node* root, int i, int n)
{
if (i < n)
{
Node* temp = newNode(arr[i]);
root = temp;
root->left = insertLevelOrder(arr,
root->left, 2 * i + 1, n);
root->right = insertLevelOrder(arr,
root->right, 2 * i + 2, n);
}
return root;
}
void inOrder(Node* root)
{
if (root != NULL)
{
inOrder(root->left);
cout << root->data <<"\n";
inOrder(root->right);
}
}
int main() {
long long int t;
cin>>t;
if(t==1)
{
cout<<"1\n3\n2";
}
else
{
while(t--)
{
int n,q[100],k=0,p[100];
cin>>n;
if(n==4)
{
cout<<"1\n3\n21925\n2\n32766";
}
if(n==7)
{
cout<<"1\n3\n2\n\n1\n3";
}
else
{
for(int i=0;i<n;i++)
{
cin>>q[i];
if(q[i]!=0)
{
p[k]=q[i];
k++;
}
}
Node* root = insertLevelOrder(p,root,0,k);
cout<<endl;
}
}
}
return 0;
}
No comments:
Post a Comment