-
Notifications
You must be signed in to change notification settings - Fork 113
/
binarytree.cpp
56 lines (51 loc) · 1007 Bytes
/
binarytree.cpp
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
#include<bits/stdc++.h>
using namespace std;
class node{
public:
int val;
node *left;
node *right;
};
void printtree(node *root){
cout<<root->val<<" ";
if(root==NULL)
{ return;
}
printtree(root->left);
printtree(root->right);
return;
}
node* addnode(int value,node *root){
if(root==NULL)
{ node* newnode =new node;
root=newnode;
newnode->val=value;
return ;
}
if(root->left==NULL){
node* newnode =new node;
root->left=newnode;
newnode->val=value;
return;
}
if(root->right==NULL){
node* newnode =new node;
root->right=newnode;
newnode->val=value;
return;
}
addnode(value,root->left);
addnode(value,root->right);
return;
}
int main(){
int arr[]={1,2,3,5,6};
node *root=NULL;
node *head=
for(int i=0;i<5;i++)
{
addnode(arr[i],root);
}
printtree(root);
return 0;
}