FastPrepLowest Common Ancestor in Ordered and Unordered Trees
Problems/Lowest Common Ancestor in Ordered and Unordered Trees

Lowest Common Ancestor in Ordered and Unordered Trees

Microsoft logoMicrosoft● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report names the BST task and its ordinary binary-tree follow-up. Their public reference statements supply the examples and bounds; node values identify the targets. Approximate source match: 95%.

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

A common ancestor has both target nodes in its subtree, allowing a node to be its own descendant. The lowest common ancestor is the common ancestor furthest from the root.

  • If isBst is true, the input is a strict binary search tree: every value in a node's left subtree is smaller and every value in its right subtree is larger.
  • If isBst is false, the input is an ordinary binary tree with no ordering guarantee. Its values must not be used to choose a search direction.

For this exercise, assume all node values are unique and both target values occur in the tree. The input uses level-order tree serialization with null for absent children.

Function

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

Examples

Example 1

root = [6,2,8,0,4,7,9,null,null,3,5]p = 2q = 8isBst = truereturn = 6
root
620435879

The targets lie on opposite sides of the root, so their lowest common ancestor is 6.

Example 2

root = [6,2,8,0,4,7,9,null,null,3,5]p = 2q = 4isBst = truereturn = 2
root
620435879

Node 2 is an ancestor of node 4 and is also its own descendant.

Example 3

root = [2,1]p = 2q = 1isBst = truereturn = 2
root
21

The root is one of the targets and contains the other target below it.

Example 4

root = [3,5,1,6,2,0,8,null,null,7,4]p = 5q = 1isBst = falsereturn = 3
root
356274108

The ordinary tree has one target in each root subtree; no BST ordering may be assumed.

Example 5

root = [3,5,1,6,2,0,8,null,null,7,4]p = 5q = 4isBst = falsereturn = 5
root
356274108

Node 5 is itself a target and is an ancestor of node 4.

Example 6

root = [1,2]p = 1q = 2isBst = falsereturn = 1
root
12

The same self-descendant rule applies to an ordinary binary tree.

Constraints

  • For this exercise, assume the finite, acyclic tree contains 2 through 10^5 nodes.
  • -10^9 <= node.val <= 10^9, with all values unique.
  • p != q, and both values occur in the tree.
  • isBst is true only when the entire tree satisfies the strict BST ordering rule; false makes no ordering guarantee.

More Microsoft Phone Screen questions

  • Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
  • Corporation Reporting LinesPHONE SCREEN · Seen Oct 2026
  • Random Forest Classifier from ScratchPHONE SCREEN · Seen Oct 2026
  • Least Recently Used CacheOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Greedy and Beam Sentence DecodingPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Top K Frequent ElementsPHONE SCREEN · Seen Sep 2026
  • 3SumPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Arithmetic Expression EvaluatorPHONE SCREEN · Seen Sep 2026
  • Employee Hierarchy Cycle and Depth AnalysisPHONE SCREEN · Seen Sep 2026
  • Most Visited String SectorsPHONE SCREEN · Seen Sep 2026

Similar questions at other companies

  • Lowest Common Ancestor in a BST and a Binary TreeGreyOrange · PHONE SCREENGreyOrange 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
  • 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

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
  • Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
All Microsoft interview questionsSee Microsoft 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…