44import java .util .List ;
55
66/**
7+ * Implement a function to check if a binary tree is a binary search tree.
8+ *
79 * @author rampatra
810 * @since 2019-02-17
911 */
@@ -13,6 +15,23 @@ private static boolean isBST(TreeNode node) {
1315 return isBST (node , new ArrayList <>());
1416 }
1517
18+ /**
19+ * This method exploits the fact that the inorder traversal of a binary search tree
20+ * results in the values being sorted in ascending order. Here, we have used a list
21+ * but if you see closely we use this list to only compare with the previous element.
22+ * Ergo, we can use an instance/class variable to store just the last element. This
23+ * will be a good optimization for space.
24+ * <p>
25+ * Time Complexity: O(n) as we touch all the nodes in the tree.
26+ * Space Complexity: O(n) as we use a list to store all the elements in the tree. If we
27+ * had used just a instance/class variable, the space complexity would have been O(log n)
28+ * as there can be up to O(log n) recursive calls as we may recurse up to the depth of
29+ * the tree. Note, the tree has to balanced though.
30+ *
31+ * @param node
32+ * @param values
33+ * @return
34+ */
1635 private static boolean isBST (TreeNode node , List <Integer > values ) {
1736 if (node == null ) return true ;
1837
@@ -27,16 +46,45 @@ private static boolean isBST(TreeNode node, List<Integer> values) {
2746 return true ;
2847 }
2948
49+ private static boolean isBSTApproach2 (TreeNode node ) {
50+ return isBSTApproach2 (node , Integer .MIN_VALUE , Integer .MAX_VALUE );
51+ }
52+
53+ /**
54+ * This approach exploits the condition that all left nodes must be less than or equal to
55+ * the current node, which must be less than all the right nodes.
56+ * <p>
57+ * Time Complexity: O(n) as we touch all the nodes in the tree.
58+ * Space Complexity: O(log n) as there are up to O(log n) recursive calls on the stack
59+ * as we may recurse up to the depth fo the tree. Note, the tree has to be balanced though.
60+ *
61+ * @param node
62+ * @param min
63+ * @param max
64+ * @return
65+ */
66+ private static boolean isBSTApproach2 (TreeNode node , int min , int max ) {
67+ if (node == null ) return true ;
68+
69+ if (node .val < min || node .val > max ) {
70+ return false ;
71+ }
72+
73+ return isBSTApproach2 (node .left , min , node .val ) && isBSTApproach2 (node .right , node .val + 1 , max );
74+ }
75+
3076 public static void main (String [] args ) {
3177 TreeNode treeRoot = new TreeNode (1 );
3278 treeRoot .left = new TreeNode (2 );
3379 treeRoot .right = new TreeNode (3 );
34- System .out .println ("Is BST: " + isBST (treeRoot ));
80+ System .out .println ("Is BST Approach 1: " + isBST (treeRoot ));
81+ System .out .println ("Is BST Approach 2: " + isBSTApproach2 (treeRoot ));
3582
3683 treeRoot = new TreeNode (2 );
3784 treeRoot .left = new TreeNode (1 );
3885 treeRoot .right = new TreeNode (3 );
39- System .out .println ("Is BST: " + isBST (treeRoot ));
86+ System .out .println ("Is BST Approach 1: " + isBST (treeRoot ));
87+ System .out .println ("Is BST Approach 2: " + isBSTApproach2 (treeRoot ));
4088
4189 treeRoot = new TreeNode (4 );
4290 treeRoot .left = new TreeNode (2 );
@@ -47,6 +95,7 @@ public static void main(String[] args) {
4795 treeRoot .right .left = new TreeNode (6 );
4896 treeRoot .right .right = new TreeNode (9 );
4997 treeRoot .right .left .right = new TreeNode (7 );
50- System .out .println ("Is BST: " + isBST (treeRoot ));
98+ System .out .println ("Is BST Approach 1: " + isBST (treeRoot ));
99+ System .out .println ("Is BST Approach 2: " + isBSTApproach2 (treeRoot ));
51100 }
52- }
101+ }
0 commit comments