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

Lowest Common Ancestor in a BST and a Binary Tree

GreyOrange logoGreyOrange● MediumFULLTIMEPHONE SCREEN

Updated Dec 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a GreyOrange SDE II interview, as the first coding question of the first round, a data structures and algorithms round. The question had two parts: find the lowest common ancestor of two nodes in a binary search tree, then find it in an ordinary binary tree whose values need not be in any order.

The report gives no examples or limits. Here one function covers both parts, with a flag that says whether the tree is a search tree, and the two nodes are named by their values. We chose our own limits, up to 10^4 nodes with values from -10^9 to 10^9, and wrote our own examples. The report describes the walk down the search tree and the recursive search for the ordinary tree; the editorial covers both.

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

You are given the root of a binary tree, two different node values p and q, and a boolean isBst. Return the value of the lowest common ancestor of the node with value p and the node with value q.

A node is a common ancestor of the two nodes if both of them are in its subtree. A node counts as part of its own subtree, so one of the two nodes can be the answer. The lowest common ancestor is the common ancestor that is farthest from the root.

The question has two parts, and isBst tells you which one a test belongs to:

  • Part (a), isBst = true: the tree is a binary search tree. For every node, all values in its left subtree are smaller than the node's value, and all values in its right subtree are larger.
  • Part (b), isBst = false: the same question for a general binary tree. The values need not follow any order, so they cannot tell you which way to go.

The tree is given in level order, with null for a missing child.

Function

lowestCommonAncestor(root: TreeNode, p: int, q: int, isBst: boolean) → int

Examples

Example 1

root = [8,3,12,1,6,10,15,null,null,4,7]p = 1q = 7isBst = truereturn = 3
root
831647121015

Both 1 and 7 are smaller than the root 8, so both are in its left subtree. At 3 they split: 1 is on the left and 7 is on the right. So the answer is 3.

Example 2

root = [8,3,12,1,6,10,15,null,null,4,7]p = 12q = 10isBst = truereturn = 12
root
831647121015

10 is the left child of 12. A node counts as part of its own subtree, so 12 itself is the lowest common ancestor.

Example 3

root = [5,9,2,4,7,null,1,null,null,3,8]p = 3q = 4isBst = falsereturn = 9
root
59473821

The values follow no order here. 4 is the left child of 9, and 3 is under 9's right child 7. The lowest node with both of them below it is 9.

Example 4

root = [5,9,2,4,7,null,1,null,null,3,8]p = 8q = 1isBst = falsereturn = 5
root
59473821

8 is in the root's left subtree and 1 is in its right subtree, so the answer is the root 5.

Constraints

  • The number of nodes in the tree is in the range [2, 10^4].
  • -10^9 <= Node.val <= 10^9
  • All Node.val are unique.
  • p != q, and both p and q are values of nodes in the tree.
  • If isBst is true, the whole tree is a binary search tree as described above.

More GreyOrange interview questions

  • Single Element in a Sorted ArrayPHONE SCREEN · Seen Dec 2026
  • Array OperationsOA · Seen Oct 2024
  • Roman Numerals in StringsOA · Seen Oct 2024
  • Triangle and PointsOA · Seen Oct 2024

Similar questions at other companies

  • Add All Larger Values in a BSTAdobe · PHONE SCREENAdobe interview questions
  • Inorder Predecessor in a BSTNutanix · PHONE SCREENNutanix interview questions
  • Shortest Path Between BST NodesGoogle · ONSITE INTERVIEWGoogle interview questions
  • Validate Binary Search TreeTokopedia · ONSITE INTERVIEWTokopedia interview questions
  • Check the Order of a Binary Search TreeCapital One · ONSITE INTERVIEWCapital One interview questions
  • 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
All GreyOrange interview questionsSee GreyOrange 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, boolean isBst) {
        // Write your code here.
    }
}
Checking account…