Coding

Find the lowest common ancestor of a binary tree.

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. The LCA is defined between two nodes p and q as the lowest node in the tree that has both p and q as descendants.

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4

Output: 5

Explanation: Node 4 is a direct descendant of Node 5 in the tree, making Node 5 the lowest common ancestor of both.

Was asked at

Practice this question with AI

First session is free - no credit card required.

Go Premium

More interviews, more skills, more success.

Community Answers

1 answer from the community

Unlock Full Access

Answer any un-answered question from our database to unlock all community answers for 30 days.

Give & Take

Practice More Questions

2 answers published. Last candidate submission on Aug 31 2026, 11:39pm PDT. Interview question asked to Software Engineers interviewing at Snap, Hitachi, Magic Leap and other companies. Original question asked: Find the lowest common ancestor of a binary tree.. Question last reported by a candidate interviewing at SAP for a Software Engineer interview on Sep 30 2026, 8:03am PDT.