-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathbinarytreebasics.cpp
More file actions
52 lines (46 loc) · 962 Bytes
/
binarytreebasics.cpp
File metadata and controls
52 lines (46 loc) · 962 Bytes
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
#include <iostream>
using namespace std;
template <typename T>
class BinaryTreeNode{
public:
T data;
BinaryTreeNode<T>* left;
BinaryTreeNode<T>* right;
BinaryTreeNode(T data){
this->data=data;
this->left=NULL;
this->right=NULL;
}
};
BinaryTreeNode<int>* takeinput(){
int data;
cin>>data;
if(data==-1){
return NULL;
}
BinaryTreeNode<int>* root=new BinaryTreeNode<int>(data);
root->left=takeinput();
root->right=takeinput();
return root;
}
void print(BinaryTreeNode<int>* root){
cout<<root->data<<": ";
if(root->left!=NULL){
cout<<root->left->data<<" ";
}
if(root->right!=NULL){
cout<<root->right->data<<" ";
}
cout<<endl;
if(root->left!=NULL){
print(root->left);
}
if(root->right!=NULL){
print(root->right);
}
}
int main() {
BinaryTreeNode<int>* root=takeinput();
print(root);
return 0;
}