Lowest Common Ancestor in a Binary Tree
Updated
Problem statement
You are given the root of a binary tree and two values p and q. Every node holds a different value, and both p and q are values of nodes in the tree.
Return the value of the lowest common ancestor of the node holding p and the node holding q. This is the deepest node that has both of them in its subtree. A node is part of its own subtree, so a node can be the lowest common ancestor of itself and another node.
The tree is a general binary tree, not a binary search tree, so the values follow no order.
Function
lowestCommonAncestor(root: TreeNode, p: int, q: int) → intExamples
Example 1
root = [10,4,7,1,9,null,3,null,null,8,2]p = 8q = 1return = 4Node 8 is the left child of 9, and 9 is the right child of 4. Node 1 is the left child of 4. So 4 has both nodes in its subtree. Neither of its children does: 1 has no children, and the subtree of 9 does not hold 1. The answer is 4.
Example 2
root = [10,4,7,1,9,null,3,null,null,8,2]p = 9q = 2return = 9Node 2 is the right child of 9. Node 9 is part of its own subtree, so its subtree holds both values, and no deeper node holds 9. The answer is 9.
Example 3
root = [10,4,7,1,9,null,3,null,null,8,2]p = 2q = 3return = 10Node 2 is in the left subtree of the root, under 4 and 9. Node 3 is in the right subtree, under 7. The two paths split at the root, so the answer is 10.
Example 4
root = [5,-3]p = -3q = 5return = 5The tree has two nodes: the root 5 and its left child -3. The root holds 5 itself and has -3 below it, so the answer is 5.
Constraints
- The number of nodes in the tree is between
2and104. -109 <= Node.val, p, q <= 109- All node values are different.
pandqare different, and both are values of nodes in the tree.- The tree has at most
500levels.
More Zillow Phone Screen questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Build a Hash TableOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Reversible Run-Length EncodingPHONE SCREEN · Seen Nov 2022
- First Unique Character in a StringOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2022
- Cumulative Fibonacci SumOA · PHONE SCREEN · Seen Aug 2021
- First N Fibonacci NumbersOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2021
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2016
- Blackjack Hand ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2016
- FactorialPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2014
- Find the Four Missing Numbers in a FileOA · PHONE SCREEN · Seen Feb 2014
Similar questions at other companies
- Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
- Count Uni-Valued SubtreesAmazon · PHONE SCREEN
- Validate Binary Search TreeLyft · PHONE SCREENLyft interview questions
- Lowest Common Ancestor in a Binary TreeGoogle · PHONE SCREENGoogle interview questions
- Check Whether a Tree Is a Sum TreeAdobe · PHONE SCREENAdobe interview questions
- Maximum Value in a Binary TreeAkuna Capital · PHONE SCREENAkuna Capital interview questions
- Sum Root-to-Leaf NumbersMeta · ONSITE INTERVIEWMeta interview questions
- Validate Binary Search TreeGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Balanced Tree Node ReportNetflix · ONSITE INTERVIEWNetflix interview questions
- Trim a Binary Search TreeBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions