Problem Submissions Solution

Lowest Common Ancestor of a Binary Tree

Difficulty: Medium

Acceptance: %

Points: 30.00

Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.

LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.

Topics

Companies

Articles

Examples:

Expected Time Complexity: O(n)

Expected Auxiliary Space: O(log n) for recursive calls.

Constraints:
  • 1 <= number of nodes <= 10^5
  • 1 <= node->data <= 10^5
Companies:
Amazon Microsoft Google Paypal Cisco + 3 more
Topics:
Tree
Locked Content
Access Restricted: Please Login to access the code editor and test cases.