-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCheckForBST.js
39 lines (29 loc) · 857 Bytes
/
CheckForBST.js
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
/**
* @param {Node} root
* @returns {boolean}
*/
// Brute Force Inorder Traversal
class Solution
{
//Function to check whether a Binary Tree is BST or not.
isBST(root)
{
//your code here
let currentValue, previousValue, flag = true;
const inorder = (root) => {
if(!root) return;
if(root.left) inorder(root.left);
if(currentValue !== undefined) {
previousValue = currentValue;
currentValue = root.data;
if(currentValue < previousValue) flag = false;
}
else {
currentValue = root.data;
}
if(root.right) inorder(root.right);
}
inorder(root);
return flag;
}
}