# Lowest Common Ancestor of a Binary Tree: Anyone know why my output is undefined?

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

EXAMPLE 1) Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.

EXAMPLE 2) Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

My code is running as expected. I used a hashMap to keep track of parent nodes as I traversed the tree. Then in a while loop, I push the parent nodes of the particular path I took to reach my p and q input nodes. In my for loop, I’m iterating through a path of parents and checking if that parent is in the second path. The first parent match is the LCA. The problem is my output is always undefined. My if condition in the for the loop is being met and I am printing the variable num correctly. Next line is a return for that num. However my output is still undefined. I can return anything from True, "string", etc. and my output doesnt change from undefined. Any insight to this issue is appreciated.

``````var lowestCommonAncestor = function(root, p, q) {
let stack = [root]
let hash = new Map()

while (stack.length) {
let node = stack.pop()

if (node.right) {
hash[node.right.val] = node.val
stack.push(node.right)
}

if (node.left) {
hash[node.left.val] = node.val
stack.push(node.left)
}
}

let path1 = [q.val, hash[q.val]]
let path2 = [p.val, hash[p.val]]

while (path1[path1.length - 1] !== root.val) {
path1.push(hash[path1[path1.length - 1]])
}

while (path2[path2.length - 1] !== root.val) {
path2.push(hash[path2[path2.length - 1]])
}

for (let i = 0; i < path1.length; i ++) {
let num = path1[i]
if (path2.indexOf(num) !== -1) {
console.log(num)
return num
}

}

};
``````

myinput:
[3,5,1,6,2,0,8,null,null,7,4], 7, 8

stdout: 3 (correct)

output: undefined

### 131 thoughts on “Lowest Common Ancestor of a Binary Tree: Anyone know why my output is undefined?”

1. I think this is among the most vital info for me. And i’m glad reading your article.
But should remark on few general things, The web site style is great,
the articles is really nice : D. Good job, cheers http://cleckleyfloors.com/

2. hello there and thank you for your info  I’ve certainly picked up anything new
from right here. I did however expertise several technical points using this site, as I experienced
to reload the web site many times previous to I could get
Well I’m adding this RSS to my email and could look out for a lot more of your respective
exciting content. Ensure that you update this again soon. http://antiibioticsland.com/Zithromax.htm

4. Since the admin of this web page is working, no doubt very
rapidly it will be famous, due to its feature contents.

5. I know this if off topic but I’m looking into
starting my own blog and was curious what all is required to get setup?

I’m assuming having a blog like yours would cost a pretty penny?
I’m not very internet savvy so I’m not 100% certain. Any suggestions or advice would be greatly appreciated.
Many thanks

6. Heya i’m for the first time here. I found this board
and I to find It truly useful & it helped me out a lot.
I hope to offer one thing back and aid others such as you aided me.

7. I’m gone to say to my little brother, that he should also
visit this blog on regular basis to take updated from most up-to-date news update.

8. Unquestionably believe that which you said. Your favorite reason appeared to
be on the net the simplest thing to be aware of. I say to you, I definitely get
annoyed while people think about worries that they just don’t
know about. You managed to hit the nail upon the top and defined
out the whole thing without having side-effects , people could take a signal.
Will probably be back to get more. Thanks

9. It’s very trouble-free to find out any topic on net as
compared to textbooks, as I found this piece of writing at this
web page.

10. A fascinating discussion is definitely worth comment.
I do believe that you need to publish more on this issue,
it may not be a taboo subject but typically folks don’t discuss such
subjects. To the next! Best wishes!!

11. A motivating discussion is definitely worth comment.
it may not be a taboo subject but typically
folks don’t discuss such subjects. To the next! Best wishes!!
cheap flights http://1704milesapart.tumblr.com/ cheap flights

12. Everything is very open with a very clear clarification of the challenges.

Thank you for sharing! scoliosis surgery https://0401mm.tumblr.com/ scoliosis surgery

13. An interesting discussion is worth comment. There’s no doubt that that you need to publish more on this subject, it might not be a taboo subject but usually folks don’t talk about such issues.
To the next! Many thanks!! scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery

the book in it or something. I think that you can do with some
pics to drive the message home a little bit, but

I will certainly be back. http://www.deinformedvoters.org/tadalafil-price

15. Pingback: ivermectin 32 mg
16. Hi there! Someone in my Facebook group shared this website
with us so I came to give it a look. I’m definitely enjoying the information. I’m bookmarking and will be tweeting this to my followers!
Great blog and excellent design. http://antiibioticsland.com/Augmentin.htm

17. Pingback: ivermectin 1gm
18. Pingback: ivermectin virus
19. Pingback: sildenafil 36
20. I am truly glad to glance at this website posts which
consists of tons of useful data, thanks for providing such data.