-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargestmaxvaluenodebst.cpp
More file actions
75 lines (65 loc) · 1.25 KB
/
largestmaxvaluenodebst.cpp
File metadata and controls
75 lines (65 loc) · 1.25 KB
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
70
71
72
73
74
75
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node*left;
struct node*right;
};
struct node*newnode(int data)
{
struct node*Node = new node();
Node->data = data;
Node->left = NULL;
Node->right = NULL;
return (Node);
}
struct node*insert(struct node*node,int data)
{
if(node==NULL)
{
return (newnode(data));
}
else
{
if(data<=node->data)
{
node->left = insert(node->left,data);
}
else
{
node->right = insert(node->right,data);
}
return node;
}
}
int maxValue(struct node*node)
{
struct node*current = node;
while(current->right!=NULL)
{
current = current->right;
}
return (current->data);
}
int minValue(struct node*node)
{
struct node*current = node;
while(current->left!=nullptr)
{
current = current->left;
}
return (current->data);
}
int main()
{
struct node*root = NULL;
root = insert(root,4);
insert(root,2);
insert(root,1);
insert(root,3);
insert(root,6);
insert(root,5);
cout << "Maximum value in BST is " << maxValue(root)<<endl;
cout << "Minimum value in BST is " << minValue(root);
}