236. Lowest Common Ancestor of a Binary Tree / 235. Lowest Common Ancestor of a Binary Search Tree
Problem Statement
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia: âThe lowest common ancestor is defined between two nodes p
and q
as the lowest node in T
that has both p
and q
as descendants (where we allow a node to be a descendant of itself).â
Example 1:
Example 2:
Example 3:
Constraints:
The number of nodes in the tree is in the range
[2, 105]
.-109 <= Node.val <= 109
All
Node.val
are unique.p != q
p
andq
will exist in the tree.
Intuition
Links
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/
Video Links
https://www.youtube.com/watch?v=_-QHfMDde90&ab_channel=takeUforward
Approach 1:
C++
Approach 2:
C++
Approach 3:
C++
Approach 4:
C++
Similar Problems
Previous103. Binary Tree Zigzag Level Order TraversalNext987. Vertical Order Traversal of a Binary Tree
Last updated