-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from safrannn/master
adding md files for leetcode/d1
- Loading branch information
Showing
64 changed files
with
2,039 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<div><p>Given the roots of two binary trees <code>p</code> and <code>q</code>, write a function to check if they are the same or not.</p> | ||
|
||
<p>Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/20/ex1.jpg" style="width: 622px; height: 182px;"> | ||
<pre><strong>Input:</strong> p = [1,2,3], q = [1,2,3] | ||
<strong>Output:</strong> true | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/20/ex2.jpg" style="width: 382px; height: 182px;"> | ||
<pre><strong>Input:</strong> p = [1,2], q = [1,null,2] | ||
<strong>Output:</strong> false | ||
</pre> | ||
|
||
<p><strong>Example 3:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/20/ex3.jpg" style="width: 622px; height: 182px;"> | ||
<pre><strong>Input:</strong> p = [1,2,1], q = [1,1,2] | ||
<strong>Output:</strong> false | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li>The number of nodes in both trees is in the range <code>[0, 100]</code>.</li> | ||
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li> | ||
</ul> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<div><p>Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).</p> | ||
|
||
<p>For example, this binary tree <code>[1,2,2,3,4,4,3]</code> is symmetric:</p> | ||
|
||
<pre> 1 | ||
/ \ | ||
2 2 | ||
/ \ / \ | ||
3 4 4 3 | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p>But the following <code>[1,2,2,null,3,null,3]</code> is not:</p> | ||
|
||
<pre> 1 | ||
/ \ | ||
2 2 | ||
\ \ | ||
3 3 | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p><b>Follow up:</b> Solve it both recursively and iteratively.</p> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<div><p>Given a binary tree, return the <i>level order</i> traversal of its nodes' values. (ie, from left to right, level by level).</p> | ||
|
||
<p> | ||
For example:<br> | ||
Given binary tree <code>[3,9,20,null,null,15,7]</code>,<br> | ||
</p><pre> 3 | ||
/ \ | ||
9 20 | ||
/ \ | ||
15 7 | ||
</pre> | ||
<p></p> | ||
<p> | ||
return its level order traversal as:<br> | ||
</p><pre>[ | ||
[3], | ||
[9,20], | ||
[15,7] | ||
] | ||
</pre> | ||
<p></p></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<div><p>Given a binary tree, return the <i>zigzag level order</i> traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).</p> | ||
|
||
<p> | ||
For example:<br> | ||
Given binary tree <code>[3,9,20,null,null,15,7]</code>,<br> | ||
</p><pre> 3 | ||
/ \ | ||
9 20 | ||
/ \ | ||
15 7 | ||
</pre> | ||
<p></p> | ||
<p> | ||
return its zigzag level order traversal as:<br> | ||
</p><pre>[ | ||
[3], | ||
[20,9], | ||
[15,7] | ||
] | ||
</pre> | ||
<p></p></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<div><p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p> | ||
|
||
<p>A binary tree's <strong>maximum depth</strong> is the number of nodes along the longest path from the root node down to the farthest leaf node.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;"> | ||
<pre><strong>Input:</strong> root = [3,9,20,null,null,15,7] | ||
<strong>Output:</strong> 3 | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre><strong>Input:</strong> root = [1,null,2] | ||
<strong>Output:</strong> 2 | ||
</pre> | ||
|
||
<p><strong>Example 3:</strong></p> | ||
|
||
<pre><strong>Input:</strong> root = [] | ||
<strong>Output:</strong> 0 | ||
</pre> | ||
|
||
<p><strong>Example 4:</strong></p> | ||
|
||
<pre><strong>Input:</strong> root = [0] | ||
<strong>Output:</strong> 1 | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li> | ||
<li><code>-100 <= Node.val <= 100</code></li> | ||
</ul></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<div><p>Given preorder and inorder traversal of a tree, construct the binary tree.</p> | ||
|
||
<p><strong>Note:</strong><br> | ||
You may assume that duplicates do not exist in the tree.</p> | ||
|
||
<p>For example, given</p> | ||
|
||
<pre>preorder = [3,9,20,15,7] | ||
inorder = [9,3,15,20,7]</pre> | ||
|
||
<p>Return the following binary tree:</p> | ||
|
||
<pre> 3 | ||
/ \ | ||
9 20 | ||
/ \ | ||
15 7</pre> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<div><p>Given inorder and postorder traversal of a tree, construct the binary tree.</p> | ||
|
||
<p><strong>Note:</strong><br> | ||
You may assume that duplicates do not exist in the tree.</p> | ||
|
||
<p>For example, given</p> | ||
|
||
<pre>inorder = [9,3,15,20,7] | ||
postorder = [9,15,7,20,3]</pre> | ||
|
||
<p>Return the following binary tree:</p> | ||
|
||
<pre> 3 | ||
/ \ | ||
9 20 | ||
/ \ | ||
15 7 | ||
</pre> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<div><p>Given a binary tree, return the <i>bottom-up level order</i> traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).</p> | ||
|
||
<p> | ||
For example:<br> | ||
Given binary tree <code>[3,9,20,null,null,15,7]</code>,<br> | ||
</p><pre> 3 | ||
/ \ | ||
9 20 | ||
/ \ | ||
15 7 | ||
</pre> | ||
<p></p> | ||
<p> | ||
return its bottom-up level order traversal as:<br> | ||
</p><pre>[ | ||
[15,7], | ||
[9,20], | ||
[3] | ||
] | ||
</pre> | ||
<p></p></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<div><p>Given an array where elements are sorted in ascending order, convert it to a height balanced BST.</p> | ||
|
||
<p>For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of <em>every</em> node never differ by more than 1.</p> | ||
|
||
<p><strong>Example:</strong></p> | ||
|
||
<pre>Given the sorted array: [-10,-3,0,5,9], | ||
|
||
One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST: | ||
|
||
0 | ||
/ \ | ||
-3 9 | ||
/ / | ||
-10 5 | ||
</pre> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<div><p>Given the <code>head</code> of a singly linked list where elements are <strong>sorted in ascending order</strong>, convert it to a height balanced BST.</p> | ||
|
||
<p>For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of <em>every</em> node never differ by more than 1.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 600px; height: 466px;"> | ||
<pre><strong>Input:</strong> head = [-10,-3,0,5,9] | ||
<strong>Output:</strong> [0,-3,9,-10,null,5] | ||
<strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre><strong>Input:</strong> head = [] | ||
<strong>Output:</strong> [] | ||
</pre> | ||
|
||
<p><strong>Example 3:</strong></p> | ||
|
||
<pre><strong>Input:</strong> head = [0] | ||
<strong>Output:</strong> [0] | ||
</pre> | ||
|
||
<p><strong>Example 4:</strong></p> | ||
|
||
<pre><strong>Input:</strong> head = [1,3] | ||
<strong>Output:</strong> [3,1] | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> | ||
<li><code>-10^5 <= Node.val <= 10^5</code></li> | ||
</ul> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<div><p>Given a binary tree, determine if it is height-balanced.</p> | ||
|
||
<p>For this problem, a height-balanced binary tree is defined as:</p> | ||
|
||
<blockquote> | ||
<p>a binary tree in which the left and right subtrees of <em>every</em> node differ in height by no more than 1.</p> | ||
</blockquote> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;"> | ||
<pre><strong>Input:</strong> root = [3,9,20,null,null,15,7] | ||
<strong>Output:</strong> true | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;"> | ||
<pre><strong>Input:</strong> root = [1,2,2,3,3,null,null,4,4] | ||
<strong>Output:</strong> false | ||
</pre> | ||
|
||
<p><strong>Example 3:</strong></p> | ||
|
||
<pre><strong>Input:</strong> root = [] | ||
<strong>Output:</strong> true | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> | ||
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li> | ||
</ul></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<div><p>Given a binary tree, find its minimum depth.</p> | ||
|
||
<p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> | ||
|
||
<p><strong>Note:</strong> A leaf is a node with no children.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;"> | ||
<pre><strong>Input:</strong> root = [3,9,20,null,null,15,7] | ||
<strong>Output:</strong> 2 | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre><strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] | ||
<strong>Output:</strong> 5 | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</code>.</li> | ||
<li><code>-1000 <= Node.val <= 1000</code></li> | ||
</ul> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<div><p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> | ||
|
||
<p>A <strong>leaf</strong> is a node with no children.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;"> | ||
<pre><strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 | ||
<strong>Output:</strong> true | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg"> | ||
<pre><strong>Input:</strong> root = [1,2,3], targetSum = 5 | ||
<strong>Output:</strong> false | ||
</pre> | ||
|
||
<p><strong>Example 3:</strong></p> | ||
|
||
<pre><strong>Input:</strong> root = [1,2], targetSum = 0 | ||
<strong>Output:</strong> false | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> | ||
<li><code>-1000 <= Node.val <= 1000</code></li> | ||
<li><code>-1000 <= targetSum <= 1000</code></li> | ||
</ul> | ||
</div> |
Oops, something went wrong.