Lowest Common Ancestor Only When Both Targets Exist
Updated
Problem statement
Given the root of a binary tree and two target values p and q, return their lowest common ancestor, but only if nodes with both values are in the tree. Otherwise, return null.
The lowest common ancestor is the deepest node whose subtree contains both targets. Every tree value is unique, so p and q point to nodes by their values. The two target values are different, and either one may be missing from the tree.
The returned TreeNode is written as the level-order list of the subtree that starts at that node.
Function
lowestCommonAncestorIfBoth(root: TreeNode, p: int, q: int) → TreeNodeExamples
Example 1
root = [3,5,1,6,2,0,8,null,null,7,4]p = 5q = 4return = [5,6,2,null,null,7,4]Removed
Moved or new
Both targets occur in the subtree rooted at value 5, and no deeper node contains them both.
Example 2
root = [1,2]p = 2q = 3return = []The node with value 3 is absent, so the method returns null. It is written here as an empty tree.
Constraints
0 <= number of nodes <= 100000.- Every tree value is a unique signed 32-bit integer.
pandqare distinct signed 32-bit integers.- The input is a finite, acyclic binary tree.
More Adobe interview questions
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Lowest Common Ancestor with Parent PointersONSITE INTERVIEW · Seen Aug 2026
- Next and Previous Lexicographic PermutationONSITE INTERVIEW · Seen Aug 2026
- Asteroid CollisionONSITE INTERVIEW · Seen Aug 2026
- Farthest Affordable Shop per QueryONSITE INTERVIEW · Seen Aug 2026
- Minimum Removals for a Nice ArrayONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Trie: Add and Search WordsONSITE INTERVIEW · Seen Jul 2026
- Base Unit ConversionsOA · Seen Nov 2026
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Maximum Root-to-Leaf and Leaf-to-Leaf Path SumsAmazon · ONSITE INTERVIEW
- Binary Tree Level Order TraversalGoogle · ONSITE INTERVIEWGoogle 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
- Height-Balanced Binary TreeExpedia · ONSITE INTERVIEWExpedia interview questions
- Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
- Validate Binary Search TreeLyft · PHONE SCREENLyft interview questions
- Maximum Value in a Binary TreeAkuna Capital · PHONE SCREENAkuna Capital interview questions
Adobe interview experiences
- Adobe New Grad OA: Unit Conversion and Code Review: 1 Experience
- Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
- Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
- Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
- Adobe New Grad MTS Interview Questions: 7 Experiences
- Adobe ML/Research Intern Interviews: 12 Experiences (Part 4 of 4)