Last active
June 13, 2018 19:04
-
-
Save pinkmomo027/ff477618c97db48d4e537787320bcd67 to your computer and use it in GitHub Desktop.
LCA
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 BNode { | |
constructor (value) { | |
this.data = value; | |
this.left = null; | |
this.right = null; | |
} | |
search(values) { | |
let c = values.indexOf(this.data) >=0 ? 1 : 0; | |
let lc = this.left ? this.left.search(values) : 0; | |
let rc = this.right? this.right.search(values) : 0; | |
return c + lc + rc; | |
} | |
searchLCA(values) { | |
if (values.indexOf(this.data) >= 0) { | |
return this; | |
} | |
//search left tree | |
if (this.left) { | |
let leftCount = this.left.search(values); | |
if (leftCount == values.length) { | |
return this.left.searchLCA(values); | |
} | |
if (leftCount == 0 && this.right) { | |
return this.right.searchLCA(values); | |
} | |
if (leftCount > 0 && leftCount < values.length) { | |
return this; | |
} | |
} | |
return "Not Found"; | |
} | |
} |
Author
pinkmomo027
commented
Jun 13, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment