File tree Expand file tree Collapse file tree 1 file changed +3
-24
lines changed Expand file tree Collapse file tree 1 file changed +3
-24
lines changed Original file line number Diff line number Diff line change 66function isBinarySearchTree ( node , min , max ) {
77 if ( ! node ) return true ;
88
9- const parent = node . data ;
10- let isBST = true , left , right ;
11-
12- if ( node . left ) {
13- left = node . left . data ;
14- isBST = isBST && left <= parent ;
15-
16- if ( min ) {
17- isBST = isBST && left > min ;
18- }
19- }
20-
21- if ( node . right ) {
22- right = node . right . data ;
23- isBST = isBST && right > parent ;
24-
25- if ( max ) {
26- isBST = isBST && right < max ;
27- }
28- }
29-
30- return isBST &&
31- isBinarySearchTree ( node . left , null , parent ) &&
32- isBinarySearchTree ( node . right , parent , null ) ;
9+ return ! ( ( min && node . data <= min ) || ( max && node . data > max ) ) &&
10+ isBinarySearchTree ( node . left , min , node . data ) &&
11+ isBinarySearchTree ( node . right , node . data , max ) ;
3312}
3413
3514module . exports = isBinarySearchTree ;
You can’t perform that action at this time.
0 commit comments