File tree Expand file tree Collapse file tree 2 files changed +132
-0
lines changed Expand file tree Collapse file tree 2 files changed +132
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Node
2
+ {
3
+ int data ;
4
+ Node left ;
5
+ Node right ;
6
+ Node (int data )
7
+ {
8
+ this .data =data ;
9
+ left =right =null ;
10
+ }
11
+ }
12
+
13
+ class BinaryTree {
14
+ Node root ;
15
+ BinaryTree ()
16
+ {
17
+ root = null ;
18
+ }
19
+ BinaryTree (int data )
20
+ {
21
+ this .root =new Node (data );
22
+ }
23
+ static int max =Integer .MIN_VALUE ;
24
+ public static int findMax (Node root )
25
+ {
26
+ if (root ==null )
27
+ {
28
+ return 0 ;
29
+ }
30
+ if (max <root .data )
31
+ {
32
+ max =root .data ;
33
+ }
34
+ findMax (root .left );
35
+ findMax (root .right );
36
+ return max ;
37
+ //return Math.max(root.data,Math.max(findMax(root.left),findMax(root.right)));
38
+ }
39
+ static int min =Integer .MAX_VALUE ;
40
+ public static int findMin (Node root )
41
+ {
42
+ if (root ==null )
43
+ {
44
+ return 0 ;
45
+ }
46
+ if (min >root .data )
47
+ {
48
+ min =root .data ;
49
+ }
50
+ findMin (root .left );
51
+ findMin (root .right );
52
+ return min ;
53
+ //return Math.min(root.data,Math.min(findMax(root.left),findMax(root.right)));
54
+ }
55
+
56
+ }
57
+
58
+ class minmaxbtree {
59
+ public static void main (String [] args ) {
60
+
61
+ BinaryTree bt =new BinaryTree (2 );
62
+ bt .root .left =new Node (3 );
63
+ bt .root .right =new Node (5 );
64
+ bt .root .left .right =new Node (9 );
65
+ bt .root .right .left =new Node (7 );
66
+ System .out .println (bt .findMax (bt .root ));
67
+ System .out .println (bt .findMin (bt .root ));
68
+
69
+ BinaryTree bt1 =new BinaryTree (1 );
70
+ bt1 .root .left =new Node (6 );
71
+ bt1 .root .right =new Node (5 );
72
+ bt1 .root .left .right =new Node (12 );
73
+ bt1 .root .right .left =new Node (7 );
74
+ System .out .println (bt1 .findMax (bt1 .root ));
75
+ System .out .println (bt1 .findMin (bt1 .root ));
76
+
77
+ }
78
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .LinkedList ;
2
+ import java .util .Queue ;
3
+
4
+ class Node
5
+ {
6
+ int data ;
7
+ Node left ;
8
+ Node right ;
9
+ Node (int data )
10
+ {
11
+ this .data =data ;
12
+ left =right =null ;
13
+ }
14
+ }
15
+
16
+ class BinaryTree {
17
+ Node root ;
18
+ BinaryTree ()
19
+ {
20
+ root = null ;
21
+ }
22
+ BinaryTree (int data )
23
+ {
24
+ this .root =new Node (data );
25
+ }
26
+
27
+ int levelPrint =1 ;
28
+ void rightView (Node root ,int level )
29
+ {
30
+ if (root ==null )
31
+ return ;
32
+ if (level ==levelPrint )
33
+ {
34
+ System .out .print (root .data +" " );
35
+ levelPrint ++;
36
+ }
37
+ rightView (root .right ,level +1 );
38
+ rightView (root .left ,level +1 );
39
+
40
+ }
41
+
42
+ }
43
+
44
+ class rightView {
45
+ public static void main (String [] args ) {
46
+
47
+ BinaryTree bt =new BinaryTree (2 );
48
+ bt .root .left =new Node (3 );
49
+ bt .root .right =new Node (5 );
50
+ bt .root .left .right =new Node (9 );
51
+ bt .root .right .left =new Node (7 );
52
+ bt .rightView (bt .root ,1 );
53
+ }
54
+ }
You can’t perform that action at this time.
0 commit comments