Class Solution
java.lang.Object
g0201_0300.s0235_lowest_common_ancestor_of_a_binary_search_tree.Solution
235 - Lowest Common Ancestor of a Binary Search Tree.<p>Easy</p>
<p>Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_top">definition of LCA on Wikipedia</a>: \u201cThe lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <strong>a node to be a descendant of itself</strong> ).\u201d</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2018/12/14/binarysearchtree_improved.png" alt="" /></p>
<p><strong>Input:</strong> root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8</p>
<p><strong>Output:</strong> 6</p>
<p><strong>Explanation:</strong> The LCA of nodes 2 and 8 is 6.</p>
<p><strong>Example 2:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2018/12/14/binarysearchtree_improved.png" alt="" /></p>
<p><strong>Input:</strong> root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4</p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong> The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> root = [2,1], p = 2, q = 1</p>
<p><strong>Output:</strong> 2</p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist in the BST.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
lowestCommonAncestor
-