MCPcopy Create free account
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / sameBsts

Function sameBsts

binary-trees/same-bsts.js:3–15  ·  view source on GitHub ↗
(arrayOne, arrayTwo)

Source from the content-addressed store, hash-verified

1// time O(n^2)
2// space O(n^2)
3function sameBsts(arrayOne, arrayTwo) {
4 if (arrayOne.length !== arrayTwo.length) return false;
5 if (arrayOne.length === 0 && arrayTwo.length === 0) return true;
6 if (arrayOne[0] !== arrayTwo[0]) return false;
7
8 // create 4 arrays O(n) time and do this n time which leads to n^2
9 const leftOne = getSmaller(arrayOne);
10 const leftTwo = getSmaller(arrayTwo);
11 const rightOne = getBiggerOrEqual(arrayOne);
12 const rightTwo = getBiggerOrEqual(arrayTwo);
13
14 return sameBsts(leftOne, leftTwo) && sameBsts(rightOne, rightTwo);
15}
16
17function getSmaller(array) {
18 const smaller = [];

Callers

nothing calls this directly

Calls 2

getSmallerFunction · 0.85
getBiggerOrEqualFunction · 0.85

Tested by

no test coverage detected