Skip to content

Commit cf6ed41

Browse files
Added README.md file for Lowest Common Ancestor of a Binary Tree
1 parent 8cf8ca5 commit cf6ed41

File tree

1 file changed

+38
-0
lines changed
  • 236-lowest-common-ancestor-of-a-binary-tree

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<h2><a href="https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree">Lowest Common Ancestor of a Binary Tree</a></h2> <img src='https://img.shields.io/badge/Difficulty-Medium-orange' alt='Difficulty: Medium' /><hr><p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
2+
3+
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: &ldquo;The 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 <b>a node to be a descendant of itself</b>).&rdquo;</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
8+
<pre>
9+
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
10+
<strong>Output:</strong> 3
11+
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
12+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
16+
<pre>
17+
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
18+
<strong>Output:</strong> 5
19+
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
20+
</pre>
21+
22+
<p><strong class="example">Example 3:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> root = [1,2], p = 1, q = 2
26+
<strong>Output:</strong> 1
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Constraints:</strong></p>
31+
32+
<ul>
33+
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
34+
<li><code>-10<sup>9</sup> &lt;= Node.val &lt;= 10<sup>9</sup></code></li>
35+
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
36+
<li><code>p != q</code></li>
37+
<li><code>p</code> and <code>q</code> will exist in the tree.</li>
38+
</ul>

0 commit comments

Comments
 (0)