Lowest Common Ancestor in a Binary Tree
Updated
Problem statement
Given a binary tree and two distinct nodes in it, return the value of their lowest common ancestor. A node counts as a descendant of itself.
Your function gets the two target nodes as their unique integer values p and q, and returns the value of the ancestor, not a node. Both targets exist in the tree. The tree is an ordinary binary tree, not necessarily a binary search tree.
Function
lowestCommonAncestorValue(root: TreeNode, p: int, q: int) → intExamples
Example 1
root = [3,5,1,6,2,0,8,null,null,7,4]p = 5q = 1return = 3One target is in the left subtree of the root and the other is in the right subtree. So the first node above both of them is the root, with value three.
Example 2
root = [3,5,1,6,2,0,8,null,null,7,4]p = 5q = 4return = 5Target q sits below target p, in the subtree under it. A node counts as its own descendant, so p is the lowest common ancestor, and its value is 5.
Example 3
root = [1,2]p = 1q = 2return = 1The root is one of the requested nodes, and the other node is its child. A node counts as its own descendant, so the root is the answer, with value one.
Constraints
- The tree contains
2through100000nodes. -1000000000 <= node.val <= 1000000000; all node values are unique.p != qand both target values occur in the tree.
More Motive Phone Screen questions
- Group AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2026
- Longest Equal-Zero-One SubarrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2026
- Valid Parenthesis StringPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2026
- Search in Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2025
- Pair Left and Right Shoes by SizePHONE SCREEN · Seen Jun 2025
- Mark a Queen’s Attack BoardPHONE SCREEN · Seen Feb 2025
- Count Product-Divisible PairsPHONE SCREEN · Seen Sep 2024
- Resolve Variable Equations with Dependency ErrorsPHONE SCREEN · Seen Jun 2024
- Average Values by Binary-Tree LevelPHONE SCREEN · Seen May 2024
- Chunked Round-Robin Row TraversalPHONE SCREEN · Seen May 2024
Similar questions at other companies
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- House Robber IIIOkta · PHONE SCREENOkta interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Validate Binary Search TreeKotak Mahindra Bank · PHONE SCREENKotak Mahindra Bank interview questions
- House Robber IIIGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
- House Robber IIIVisa · PHONE SCREENVisa interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions