-
Notifications
You must be signed in to change notification settings - Fork 54
Closed
Description
🚀 Feature Request: Add "Lowest Common Ancestor (LCA) of a Binary Tree" (LeetCode #236)
🧩 Description
I would like to contribute the implementation of the Lowest Common Ancestor (LCA) of a Binary Tree problem.
This algorithm finds the lowest (or deepest) node in a binary tree that is an ancestor of two given nodes.
🧠 Problem Statement
Given a binary tree and two nodes p
and q
, find their lowest common ancestor (LCA).
The LCA of two nodes p
and q
is the lowest node in the tree that has both p
and q
as descendants (a node can be a descendant of itself).
📘 LeetCode Reference: Problem #236 – Lowest Common Ancestor of a Binary Tree
⚙️ Approach
- Use a recursive approach:
- If the current node is
null
, returnnull
. - If the current node matches either
p
orq
, return the current node. - Recursively search for LCA in the left and right subtrees.
- If both sides return non-null, the current node is the LCA.
- Otherwise, return the non-null result.
- If the current node is
🧮 Time and Space Complexity
- Time Complexity: O(N), where N is the number of nodes in the tree.
- Space Complexity: O(H), where H is the height of the tree (due to recursion stack).
💻 Language
- Java
✅ Checklist
- I have read the contribution guidelines.
- My code follows the repository’s coding style.
- I will add proper test cases and comments.
Metadata
Metadata
Assignees
Labels
No labels