Skip to content

Same Bsts

Category: Binary Search Trees

```

Problem Statement

Same BSTs An array of integers is said to represent the Binary Search Tree (BST) obtained by inserting each integer in the array, from left to right, into the BST. Write a function that takes in two arrays of integers and determines whether these arrays represent the same BST. Note that you're not allowed to construct any BSTs in your code. A BST is a Binary Tree that consists only of BST nodes. A node is said to be a valid BST node if and only if it satises the BST property: its value is strictly greater than the values of every node to its left; its value is less than or equal to the values of every node to its right; and its children nodes are either valid BST nodes themselves or None / null . Sample Input arrayOne = [10, 15, 8, 12, 94, 81, 5, 2, 11] arrayTwo = [10, 8, 5, 15, 2, 12, 11, 94, 81] Sample Output true // both arrays represent the BST below / \ 15 / / \ 12 94 / / / 11 81 Hints Hint 1 You can immediately conclude that the input arrays don't represent the same BST if their rst values aren't equal to each other, since their rst values represent the root of the BST. Similarly, you can conclude this if their lengths are dierent. If their rst values are equal to each other and their lengths are the same, what should your next step be? Hint 2 Given an array of integers, all of the values in the array that are smaller than the rst value in the array are located in the left subtree of the BST that the array represents, and all of the values in the array that are greater than or equal to the rst value in the array are located in the right subtree of the BST that the array represents. Use this fact and Hint #1 to recursively determine whether all subtrees in the BSTs represented by the arrays are equal to each other. Hint 3 Write a recursive function that takes in two arrays of integers. If the rst values of the arrays aren't equal to each other or if the arrays don't have the same length, the arrays don't represent the same BST. If the rst values and lengths are equal to each other, respectively, perform the following actions on both arrays: gather all integers that are smaller than the rst integer; these form a new array that represents the left subtree of the relevant BST; gather all integers that are greater than or equal to the rst integer; these form a new array that represents the right subtree of the relevant BST. Call the recursive function twice: once with the two left-subtree arrays and once with the two right-subtree arrays. Hint 4 Do you actually need to create all of the auxiliary arrays mentioned in Hint #3? Optimal Space & Time Complexity O(n^2) time | O(d) space - where n is the number of nodes in each array, respectively, and d is the depth of the BST that they represent

```

Approach & Solution

Solution 1

``` Solution 1 Solution 2 import java.util.*; class Program { // O(n^2) time | O(d) space - where n is the number of // nodes in each array, respectively, and d is the depth // of the BST that they represent public static boolean sameBsts(List arrayOne, List arrayTwo) { return areSameBsts(arrayOne, arrayTwo, 0, 0, Integer.MIN_VALUE, Integer.MAX_VALUE); } public static boolean areSameBsts( List arrayOne, List arrayTwo, int rootIdxOne, int rootIdxTwo, int minVal, int maxVal) { if (rootIdxOne == -1 || rootIdxTwo == -1) return rootIdxOne == rootIdxTwo; if (arrayOne.get(rootIdxOne).intValue() != arrayTwo.get(rootIdxTwo).intValue()) return false; int leftRootIdxOne = getIdxOfFirstSmaller(arrayOne, rootIdxOne, minVal); int leftRootIdxTwo = getIdxOfFirstSmaller(arrayTwo, rootIdxTwo, minVal); int rightRootIdxOne = getIdxOfFirstBiggerOrEqual(arrayOne, rootIdxOne, maxVal); int rightRootIdxTwo = getIdxOfFirstBiggerOrEqual(arrayTwo, rootIdxTwo, maxVal); int currentValue = arrayOne.get(rootIdxOne); boolean leftAreSame = areSameBsts(arrayOne, arrayTwo, leftRootIdxOne, leftRootIdxTwo, minVal, currentValue); boolean rightAreSame = areSameBsts(arrayOne, arrayTwo, rightRootIdxOne, rightRootIdxTwo, currentValue, maxVal); return leftAreSame && rightAreSame; } public static int getIdxOfFirstSmaller(List array, int startingIdx, int minVal) { // Find the index of the first smaller value after the startingIdx. // Make sure that this value is greater than or equal to the minVal, // which is the value of the previous parent node in the BST. If it // isn't, then that value is located in the left subtree of the // previous parent node. for (int i = startingIdx + 1; i < array.size(); i++) { if (array.get(i).intValue() < array.get(startingIdx).intValue() && array.get(i).intValue() >= minVal) return i; } return -1; } public static int getIdxOfFirstBiggerOrEqual(List array, int startingIdx, int maxVal) { // Find the index of the first bigger/equal value after the startingIdx. // Make sure that this value is smaller than maxVal, which is the value // of the previous parent node in the BST. If it isn't, then that value // is located in the right subtree of the previous parent node. for (int i = startingIdx + 1; i < array.size(); i++) { if (array.get(i).intValue() >= array.get(startingIdx).intValue() && array.get(i).intValue() < maxVal) return i; } return -1; } }

Solution 2

``` Solution 1 Solution 2 2 3 import java.util.*; 4 5 class Program { 6 // O(n^2) time | O(d) space - where n is the number of 7 // nodes in each array, respectively, and d is the depth 8 // of the BST that they represent 9 public static boolean sameBsts(List arrayOne, List arrayTwo) { 10 return areSameBsts(arrayOne, arrayTwo, 0, 0, Integer.MIN_VALUE, Integer.MAX_VALUE); 11 } 12 13 public static boolean areSameBsts( 14 List arrayOne, 15 List arrayTwo, 16 int rootIdxOne, 17 int rootIdxTwo, 18 int minVal, 19 int maxVal) { 20 if (rootIdxOne == -1 || rootIdxTwo == -1) return rootIdxOne == rootIdxTwo; 21 22 if (arrayOne.get(rootIdxOne).intValue() != arrayTwo.get(rootIdxTwo).intValue()) return false; 23 24 int leftRootIdxOne = getIdxOfFirstSmaller(arrayOne, rootIdxOne, minVal); 25 int leftRootIdxTwo = getIdxOfFirstSmaller(arrayTwo, rootIdxTwo, minVal); 26 int rightRootIdxOne = getIdxOfFirstBiggerOrEqual(arrayOne, rootIdxOne, maxVal); 27 int rightRootIdxTwo = getIdxOfFirstBiggerOrEqual(arrayTwo, rootIdxTwo, maxVal); 28 29 int currentValue = arrayOne.get(rootIdxOne); 30 boolean leftAreSame = 31 areSameBsts(arrayOne, arrayTwo, leftRootIdxOne, leftRootIdxTwo, minVal, currentValue); 32 boolean rightAreSame = 33 areSameBsts(arrayOne, arrayTwo, rightRootIdxOne, rightRootIdxTwo, currentValue, maxVal); 34 35 return leftAreSame && rightAreSame; 36 } 37 38 public static int getIdxOfFirstSmaller(List array, int startingIdx, int minVal) { 39 // Find the index of the first smaller value after the startingIdx. 40 // Make sure that this value is greater than or equal to the minVal, 41 // which is the value of the previous parent node in the BST. If it 42 // isn't, then that value is located in the left subtree of the 43 // previous parent node. 44 for (int i = startingIdx + 1; i < array.size(); i++) { 45 if (array.get(i).intValue() < array.get(startingIdx).intValue()

Solution 3

``` Solution 1 Solution 2 import java.util.*; class Program { // O(n^2) time | O(n^2) space - where n is the number of // nodes in each array, respectively public static boolean sameBsts(List arrayOne, List arrayTwo) { if (arrayOne.size() != arrayTwo.size()) return false; if (arrayOne.size() == 0 && arrayTwo.size() == 0) return true; if (arrayOne.get(0).intValue() != arrayTwo.get(0).intValue()) return false; List leftOne = getSmaller(arrayOne); List leftTwo = getSmaller(arrayTwo); List rightOne = getBiggerOrEqual(arrayOne); List rightTwo = getBiggerOrEqual(arrayTwo); return sameBsts(leftOne, leftTwo) && sameBsts(rightOne, rightTwo); } public static List getSmaller(List array) { List smaller = new ArrayList(); for (int i = 1; i < array.size(); i++) { if (array.get(i).intValue() < array.get(0).intValue()) smaller.add(array.get(i)); } return smaller; } public static List getBiggerOrEqual(List array) { List biggerOrEqual = new ArrayList(); for (int i = 1; i < array.size(); i++) { if (array.get(i).intValue() >= array.get(0).intValue()) biggerOrEqual.add(array.get(i)); } return biggerOrEqual; } }

```

Test Cases

``` ▲

``` Test Case 1 { "arrayOne": [10, 15, 8, 12, 94, 81, 5, 2, 11], "arrayTwo": [10, 8, 5, 15, 2, 12, 11, 94, 81] } Test Case 2 {"arrayOne": [1, 2, 3, 4, 5, 6, 7], "arrayTwo": [1, 2, 3, 4, 5, 6, 7]} Test Case 3 {"arrayOne": [7, 6, 5, 4, 3, 2, 1], "arrayTwo": [7, 6, 5, 4, 3, 2, 1]} Test Case 4 { "arrayOne": [10, 15, 8, 12, 94, 81, 5, 2], "arrayTwo": [10, 8, 5, 15, 2, 12, 94, 81] } Test Case 5 { "arrayOne": [10, 15, 8, 12, 94, 81, 5, 2], "arrayTwo": [11, 8, 5, 15, 2, 12, 94, 81] } Test Case 6 { "arrayOne": [10, 15, 8, 12, 94, 81, 5, 2, -1, 100, 45, 12, 8, -1, 8, 2, -34], "arrayTwo": [10, 8, 5, 15, 2, 12, 94, 81, -1, -1, -34, 8, 2, 8, 12, 45, 100] } Test Case 7 { "arrayOne": [10, 15, 8, 12, 94, 81, 5, 2, -1, 101, 45, 12, 8, -1, 8, 2, -34], "arrayTwo": [10, 8, 5, 15, 2, 12, 94, 81, -1, -1, -34, 8, 2, 8, 12, 45, 100] } Test Case 8 { "arrayOne": [5, 2, -1, 100, 45, 12, 8, -1, 8, 10, 15, 8, 12, 94, 81, 2, -34], "arrayTwo": [5, 8, 10, 15, 2, 8, 12, 45, 100, 2, 12, 94, 81, -1, -1, -34, 8] } Test Case 9 { "arrayOne": [10, 15, 8, 12, 94, 81, 5, 2, -1, 100, 45, 12, 9, -1, 8, 2, -34], "arrayTwo": [10, 8, 5, 15, 2, 12, 94, 81, -1, -1, -34, 8, 2, 9, 12, 45, 100] } Test Case 10 {"arrayOne": [1, 2, 3, 4, 5, 6, 7, 8], "arrayTwo": [1, 2, 3, 4, 5, 6, 7]} Test Case 11 {"arrayOne": [7, 6, 5, 4, 3, 2, 1], "arrayTwo": [7, 6, 5, 4, 3, 2, 1, 0]} Test Case 12 { "arrayOne": [10, 15, 8, 12, 94, 81, 5, 2, 10], "arrayTwo": [10, 8, 5, 15, 2, 10, 12, 94, 81] } Test Case 13 { "arrayOne": [50, 76, 81, 23, 23, 23, 657, 56, 12, -1, 3], "arrayTwo": [50, 23, 76, 23, 23, 12, 56, 81, -1, 3, 657] }