FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java-coding-ninjas/binarySearchTree1/CheckBSTOrNot.java at master · avinashbest/java-coding-ninjas · GitHub
avinashbest
/
java-coding-ninjas
Public
Notifications
You must be signed in to change notification settings
Fork
11
Star
18
Code
Issues
0
Pull requests
2
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java-coding-ninjas
/
binarySearchTree1
/
CheckBSTOrNot.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
75 lines (54 loc) · 2.04 KB
Breadcrumbs
java-coding-ninjas
/
binarySearchTree1
/
CheckBSTOrNot.java
Copy path
File metadata and controls
75 lines (54 loc) · 2.04 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
binarySearchTree1
;
import
static
binarySearchTree1
.
SearchInBST
.
levelWiseInput
;
class
IsBSTReturn
{
int
minimum
;
int
maximum
;
boolean
isBST
;
public
IsBSTReturn
(
int
minimum
,
int
maximum
,
boolean
isBST
) {
this
.
minimum
=
minimum
;
this
.
maximum
=
maximum
;
this
.
isBST
=
isBST
;
}
}
public
class
CheckBSTOrNot
{
public
static
boolean
checkBST
(
TreeNode
<
Integer
>
root
) {
if
(
root
==
null
)
return
true
;
int
leftMax
=
maximum
(
root
.
left
);
if
(
leftMax
>=
root
.
data
) {
return
false
;
}
int
rightMin
=
minimum
(
root
.
right
);
if
(
rightMin
<
root
.
data
) {
return
false
;
}
return
checkBST
(
root
.
left
) &&
checkBST
(
root
.
right
);
}
private
static
int
minimum
(
TreeNode
<
Integer
>
root
) {
if
(
root
==
null
)
return
Integer
.
MAX_VALUE
;
int
left
=
minimum
(
root
.
left
);
int
right
=
minimum
(
root
.
right
);
return
Math
.
min
(
root
.
data
,
Math
.
min
(
left
,
right
));
}
private
static
int
maximum
(
TreeNode
<
Integer
>
root
) {
if
(
root
==
null
)
return
Integer
.
MIN_VALUE
;
int
left
=
maximum
(
root
.
left
);
int
right
=
maximum
(
root
.
right
);
return
Math
.
max
(
root
.
data
,
Math
.
max
(
left
,
right
));
}
public
static
IsBSTReturn
isBSTImproved
(
TreeNode
<
Integer
>
root
) {
if
(
root
==
null
) {
return
new
IsBSTReturn
(
Integer
.
MAX_VALUE
,
Integer
.
MIN_VALUE
,
true
);
}
var
left
=
isBSTImproved
(
root
.
left
);
var
right
=
isBSTImproved
(
root
.
right
);
int
minimum
=
Math
.
min
(
root
.
data
,
Math
.
min
(
left
.
minimum
,
right
.
minimum
));
int
maximum
=
Math
.
max
(
root
.
data
,
Math
.
max
(
left
.
maximum
,
right
.
maximum
));
boolean
isBST
=
left
.
maximum
<
root
.
data
||
right
.
minimum
>=
root
.
data
||
left
.
isBST
||
right
.
isBST
;
return
new
IsBSTReturn
(
minimum
,
maximum
,
isBST
);
}
public
static
void
main
(
String
[]
args
) {
var
root
=
levelWiseInput
();
// System.out.println(checkBST(root));
System
.
out
.
println
(
isBSTImproved
(
root
).
isBST
);
}
}
Back
|
FazBrowse Home
|
New Git URL