-
Notifications
You must be signed in to change notification settings - Fork 0
/
array to inorder,preorder,postorder
69 lines (54 loc) · 1.3 KB
/
array to inorder,preorder,postorder
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<bits/stdc++.h>
using namespace std;
#define read() freopen("input.txt", "r", stdin);
#define write() freopen("output.txt", "w", stdout);
#define ll long long int
struct Node
{
int data;
Node* left, * right;
};
Node* CreateNode(int data)
{
Node* node = new Node();
node->data = data;
node->left =NULL;
node->right = NULL;
return node;
}
// Function to insert nodes in level order
Node* insertLevelOrder(int arr[], Node* root, int i, int n)
{
if (i < n)
{
Node* temp = CreateNode(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;
}
void inOrder(Node* root)
{
if (root != NULL)
{
//cout<<root->data<<" ";
inOrder(root->left);
// cout << root->data <<" ";
inOrder(root->right);
cout<<root->data<<" ";
}
}
int main()
{
#ifdef asiuzzaman
read(); write();
#endif
int arr[] = { 1, 2, 3, 4, 5, 6, 6, 6, 6 };
int n = sizeof(arr)/sizeof(arr[0]);
Node *root;
root = insertLevelOrder(arr, root, 0, n);
inOrder(root);
}