forked from kapoorp99/hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarytree.2.cpp
64 lines (57 loc) · 1.14 KB
/
binarytree.2.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
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
template <typename T>
class BinaryTreeNode
{
public:
T data;
BinaryTreeNode *left;
BinaryTreeNode *right;
BinaryTreeNode(T a)
{
data = a;
left = NULL;
right = NULL;
}
};
BinaryTreeNode<int> *TakeInput()
{
int rootdata;
cout << "enter the rootdata " << endl;
cin >> rootdata;
if (rootdata == -1)
{
return NULL;
}
BinaryTreeNode<int> *root = new BinaryTreeNode<int>(rootdata);
BinaryTreeNode<int> *leftchild = TakeInput();
BinaryTreeNode<int> *rightchild = TakeInput();
root->left = leftchild;
root->right = rightchild;
return root;
}
void PrintTree(BinaryTreeNode<int> *root)
{
if (root == NULL)
{
return;
}
cout << root->data << " : ";
if (root->left != NULL)
{
cout << "L." << root->left->data << " ";
}
if (root->right != NULL)
{
cout << "R." << root->right->data << " ";
}
cout << endl;
PrintTree(root->left);
PrintTree(root->right);
}
int main()
{
BinaryTreeNode<int> *root = TakeInput();
PrintTree(root);
return 0;
}