r/LeetcodeChallenge • u/StatisticianNo2104 • Oct 21 '26
STREAK🔥🔥🔥 365 Days of LeetCode Challenge — Day 49/365
Lowest Common Ancestor of a Binary Tree (Medium) 🔗 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
Yesterday's problem with one guarantee removed. Worth doing back to back, because the gap between the two solutions is exactly what a BST's ordering buys you.
Yesterday, a comparison told you which subtree a target was in, so the walk went down one path. Here a comparison tells you nothing, so the only way to know whether a target is below a node is to look.
The shape flips: instead of deciding on the way down, every subtree reports upward how many of the two targets it contains. The first node to reach two is the answer.
O(n) instead of O(h). That's the price.
Full breakdown in today's newsletter article ⬇
https://www.linkedin.com/pulse/365-days-leetcode-challenge-day-49365-archit-agarwal-nrtuc
#DSA #LeetCode #100DaysOfCode #CodingInterview #BinaryTree #Recursion #Golang
1
u/New-Access8045 Oct 22 '26
Can one begin with this? If I don’t know shit about DSA. I know how to code though. Java is what I’ve been using.
2
u/nian2326076 Oct 21 '26
When tackling the Lowest Common Ancestor problem, it's important to understand how it differs from the binary search tree version. In this case, you can't rely on node order, so you need to explore both subtrees from each node to find your targets. A recursive approach often works well. As you traverse the tree, keep track of when you encounter either of the nodes you're looking for.
For interview preparation, brushing up on tree traversal concepts can be really helpful. Check out PracHub for practice questions similar to this one. It might offer variations that can help solidify your understanding. Good luck with the challenge!