FastPrepLowest Common Ancestor in a Binary Tree
Problems/Lowest Common Ancestor in a Binary Tree

Lowest Common Ancestor in a Binary Tree

Zillow logoZillow● MediumFULLTIMENEW GRADINTERNPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Asked at Zillow from about 2013 to 2019, mostly in phone screens, for software intern and new grad roles, other software roles with no level given, and once for a machine learning engineer role. Candidates were handed a binary tree with two of its nodes and asked which node is the deepest one above both. One was told the tree is not a search tree and then asked to make the answer faster. Another solved it with recursion and was asked for other ways. Another had to build the tree first and then check the answer with their own tests.

The reports do not say how the tree is given, whether values repeat, or whether both nodes are sure to be in the tree. Here the tree comes as a level-order list with unique values, both values are in the tree, and you return the ancestor's value. The tree is limited to 500 levels so a recursive answer fits in every language's call stack.

Our version is about 85% the same as the reported question.

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) → int

Examples

Example 1

root = [10,4,7,1,9,null,3,null,null,8,2]p = 8q = 1return = 4
root
104198273

Node 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 = 9
root
104198273

Node 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 = 10
root
104198273

Node 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 = 5
root
5-3

The 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 2 and 104.
  • -109 <= Node.val, p, q <= 109
  • All node values are different.
  • p and q are different, and both are values of nodes in the tree.
  • The tree has at most 500 levels.

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
All Zillow interview questionsSee Zillow hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int lowestCommonAncestor(TreeNode root, int p, int q) {
        // Write your code here.
    }
}
Checking account…