Q. Complete the method “isValidBST(Node root)” which takes a “Tree node” as an input to evaluate if the input is a valid BST (i.e. Binary Search Tree)?
|
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 |
public class BinarySearchTree { public static boolean isValidBST(Node root) { throw new RuntimeException("To be completed"); } public static void main(String[] args) { Node n4 = new Node(2, null, null); Node n5 = new Node(4, null, null); Node n6 = new Node(6, null, null); Node n7 = new Node(8, null, null); Node n1 = new Node(3, n4, n5); Node n3 = new Node(7, n6, n7); //root node Node n2 = new Node(5, n1, n3); System.out.println(isValidBST(n2)); } } class Node { public int value; public Node left, right; public Node(int value, Node left, Node right) { this.value = value; this.left = left; this.right = right; } } |
A. Detailed answers on recursive & iterative solutions. What is a BST?…