Skip to content

Commit 2f27dc5

Browse files
committed
Add the .cpp extension
1 parent 3af4249 commit 2f27dc5

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//construct BST from its level order traversal
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
struct node
5+
{
6+
int data;
7+
struct node *lchild, *rchild;
8+
};
9+
10+
11+
// function to create node
12+
struct node* createNode(int key)
13+
{
14+
struct node *newNode =
15+
(struct node*)malloc(sizeof(struct node));
16+
newNode->data = key;
17+
newNode->lchild = newNode->rchild = NULL;
18+
return newNode;
19+
}
20+
// function to construct a BST from its level order traversal
21+
struct node *levelOrder(struct node *root , int key)
22+
{
23+
if(root==NULL){
24+
root = createNode(key);
25+
return root;
26+
}
27+
if(key <= root->data)
28+
root->lchild = levelOrder(root->lchild, key);
29+
else
30+
root->rchild = levelOrder(root->rchild, key);
31+
return root;
32+
}
33+
//function to construct BST
34+
struct node* constructBst(int a[], int n)
35+
{
36+
if(n==0)return NULL;
37+
struct node *root =NULL;
38+
39+
for(int i=0;i<n;i++)
40+
root = levelOrder(root , a[i]);
41+
42+
return root;
43+
}
44+
// function to get the inorder traversal of BST
45+
void inorder(struct node* root)
46+
{
47+
if (!root)
48+
return;
49+
50+
inorder(root->lchild);
51+
cout << root->data << " ";
52+
inorder(root->rchild);
53+
}
54+
55+
56+
// Driver program
57+
int main()
58+
{
59+
int a[] = {100,50,200,25,75,350};
60+
int n = sizeof(a) / sizeof(a[0]);
61+
62+
struct node *root = constructBst(a, n);
63+
64+
cout << "Inorder Traversal: ";
65+
inorder(root);
66+
return 0;
67+
}

0 commit comments

Comments
 (0)