Created
January 21, 2022 20:54
-
-
Save hasangenc0/18bf4dc9c99ba56e2714ef735c75fa2b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
value = null; | |
leftChild = null; | |
rightChild = null; | |
constructor(value) { | |
this.value = value | |
} | |
} | |
/** | |
* Time Complexity: O(n) | |
* Space Complexity: O(0) | |
*/ | |
function compareInorder(tree1, tree2) { | |
if (!tree1 && !tree2) return true; | |
if (tree1 && !tree2) return false; | |
if (!tree1 && tree2) return false; | |
if (compareInorder(tree1.leftChild, tree2.leftChild) === false) { | |
return false; | |
} | |
if (tree1.value != tree2.value) { | |
return false; | |
} | |
if (compareInorder(tree1.rightChild, tree2.rightChild) === false) { | |
return false; | |
} | |
return true; | |
} | |
const tree1 = new Node(5); | |
tree1.leftChild = new Node(4); | |
tree1.rightChild = new Node(3); | |
tree1.rightChild.rightChild = new Node(2); | |
tree1.rightChild.rightChild.leftChild = new Node(1); | |
const tree2 = new Node(5); | |
tree2.leftChild = new Node(4); | |
tree2.rightChild = new Node(3); | |
tree2.rightChild.rightChild = new Node(2); | |
tree2.rightChild.rightChild.leftChild = new Node(1); | |
console.log(compareInorder(tree1, tree2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment