Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. function sameBST(arr1, arr2) {
  2.  
  3. if (arr1.length !== arr2.length) return false;
  4. if (!arr1.length && !arr2.length) return true;
  5.  
  6. const left1 = [];
  7. const left2 = [];
  8. const right1 = [];
  9. const right2 = [];
  10. const root1 = arr1[0];
  11. const root2 = arr2[0];
  12.  
  13. if (root1 !== root2)
  14. return false;
  15.  
  16.  
  17.  
  18. for (let i = 1; i < arr1.length; i++) {
  19. arr1[i] < root1 ? left1.push(arr1[i]) : right1.push(arr1[i]);
  20. arr2[i] < root2 ? left2.push(arr2[i]) : right2.push(arr2[i]);
  21. }
  22.  
  23. return sameBST(left1, left2) && sameBST(right1, right2);
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement