-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathbinaryTree_1.cpp
55 lines (46 loc) · 1 KB
/
binaryTree_1.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
#include <iostream>
using namespace std;
template <typename T>
class BinaryTreeNode
{
public:
T data;
BinaryTreeNode* left;
BinaryTreeNode* right;
BinaryTreeNode(T data){
this->data=data;
left=NULL;
right=NULL;
}
~BinaryTreeNode(){
delete left;
delete right;
}
};
//printing binary tree
void printTree(BinaryTreeNode<int>* root)
{
if(root==NULL)
return;
cout<<root->data<<":";
if(root->left){
cout<<"L"<<root->left->data<<" ";
}
if(root->right){
cout<<"R"<<root->right->data<<" ";
}
cout<<endl;
printTree(root->left);
printTree(root->right);
}
int main()
{
BinaryTreeNode<int>* root=new BinaryTreeNode<int>(1);
BinaryTreeNode<int>* node1=new BinaryTreeNode<int>(2);
BinaryTreeNode<int>* node2=new BinaryTreeNode<int>(3);
root->left=node1;
root->right=node2;
printTree(root);
delete root;
return 0;
}