forked from joharbatta/DataStructure-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminmaxbtree.java
More file actions
78 lines (72 loc) · 1.62 KB
/
Copy pathminmaxbtree.java
File metadata and controls
78 lines (72 loc) · 1.62 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
76
77
78
class Node
{
int data;
Node left;
Node right;
Node(int data)
{
this.data=data;
left=right=null;
}
}
class BinaryTree{
Node root;
BinaryTree()
{
root = null;
}
BinaryTree(int data)
{
this.root=new Node(data);
}
static int max=Integer.MIN_VALUE;
public static int findMax(Node root)
{
if(root==null)
{
return 0;
}
if(max<root.data)
{
max=root.data;
}
findMax(root.left);
findMax(root.right);
return max;
//return Math.max(root.data,Math.max(findMax(root.left),findMax(root.right)));
}
static int min=Integer.MAX_VALUE;
public static int findMin(Node root)
{
if(root==null)
{
return 0;
}
if(min>root.data)
{
min=root.data;
}
findMin(root.left);
findMin(root.right);
return min;
//return Math.min(root.data,Math.min(findMax(root.left),findMax(root.right)));
}
}
class minmaxbtree{
public static void main(String[] args) {
BinaryTree bt=new BinaryTree(2);
bt.root.left=new Node(3);
bt.root.right=new Node(5);
bt.root.left.right=new Node(9);
bt.root.right.left=new Node(7);
System.out.println(bt.findMax(bt.root));
System.out.println(bt.findMin(bt.root));
BinaryTree bt1=new BinaryTree(1);
bt1.root.left=new Node(6);
bt1.root.right=new Node(5);
bt1.root.left.right=new Node(12);
bt1.root.right.left=new Node(7);
System.out.println(bt1.findMax(bt1.root));
System.out.println(bt1.findMin(bt1.root));
}
}