Home › Company-wise questions › Deloitte › Lowest Common Ancestor of BST ← Deloitte bank
Deloitte · Question 67 · Trees
Lowest Common Ancestor of BST Trees Java code ⏱ O(h) 💾 O(1)
Service-based Published and maintained by DSA-topics · Last updated 30 November 2026 · Report an error
About this bank These are classic technical-interview concepts compiled from publicly circulated company-wise question sets, rewritten with original explanations and compiler-ready solutions for educational practice; inclusion does not imply endorsement by the company. Spotted an error? Tell us .
Problem For a BST, compare node values: if both less than root go left, both greater go right, else root is the LCA.
Approach Walk down using BST order until the split point.
Time O(h)Space O(1)
How it works 1 Both targets left means go left; both right means go right. 2 The split node is the LCA. Sample input / output Input:
root = [6, 2, 8, 0, 4, 7, 9, null, null, 3, 5], p = 2, q = 8
Output:
6 Constraints BST property holds; p and q exist.
Solution — read it, then walk it Flowchart Pseudocode
Time O(h)Space O(1)Loops 4Branches 9Returns 4source · Java
Scroll sideways for wide charts — the view follows the active step. ⤢ Expand
↻ repeat ↻ repeat ↻ repeat ↻ repeat START import java.util.* DEFINE class Main DEFINE class TreeNode DEFINE val TreeNode left, right TreeNode(int v) { val = v; } DEFINE TreeNode buildTree(Integer[] values) if (values.length == 0 || values[0] == null) return null TreeNode root = new TreeNode(values[0]) java.util.Queue<TreeNode> q = new java.util.LinkedList<>() q.offer(root) DEFINE i = 1 while (!q.isEmpty() && i < values.length) TreeNode node = q.poll() if (i < values.length && values[i] != null) { node.left = ne… i++ if (i < values.length && values[i] != null) { node.right = ne… i++ RETURN root DEFINE String levelOrder(TreeNode root) if (root == null) return "[]" StringBuilder sb = new StringBuilder("[") java.util.Queue<TreeNode> q = new java.util.LinkedList<>() q.offer(root) DEFINE first = true while (!q.isEmpty()) TreeNode node = q.poll() if (node == null) continue if (!first) sb.append(", ") first = false sb.append(node.val) q.offer(node.left) q.offer(node.right) RETURN sb.append("]").toString() DEFINE static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNod… while (root != null) if (p.val < root.val && q.val < root.val) root = root.left else if (p.val > root.val && q.val > root.val) root … ELSE RETURN null DEFINE TreeNode find(TreeNode root, int val) while (root != null && root.val != val) root = (val < root.val) ? root.left : root.right RETURN root DEFINE static void main(String[] args) start / end statement condition loop returns definition← Q66 · Construct Binary Tree from Inorder and Postorder Q68 · Lowest Common Ancestor of Binary Tree → Solution code Core logic highlighted in green
Language Python Javasource C++ C Core logic Full code Copy
Recommended resources Affiliate links · may earn us a commission
Classic DSA interview books The reference books interviewers themselves learned from — great next to the visualizers. See the books → Structured coding courses Video courses that pair well with visual practice — pick one language and go deep. Browse courses → Cloud hosting for side projects Ship the portfolio project that gets you hired — free credits for new accounts. Claim credits →