Lowest Common Ancestor of a Binary Tree III

Try to solve the Lowest Common Ancestor of a Binary Tree III problem.

Statement

You are given two nodes, p and q. The task is to return their lowest common ancestor (LCA). Both nodes have a reference to their parent node. The tree’s root is not provided; you must use the parent pointers to find the nodes’ common ancestor.

Note: The lowest common ancestor of two nodes, p and q, is the lowest node in the binary tree, with both p and q as descendants.

In a tree, a descendant of a node is any node reachable by following edges downward from that node, including the node itself.

Constraints:

  • 104-10^4 \leqNode.data 104\leq 10^4

  • The number of nodes in the tree is in the range [2,500].[2, 500].

  • All Node.data are unique.

  • p != q

  • Both p and q are present in the tree.

Example

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.