Skip to content

Commit

Permalink
Merge pull request #22 from safrannn/master
Browse files Browse the repository at this point in the history
adding md files for leetcode/d1
  • Loading branch information
warycat authored Jan 21, 2021
2 parents 56cf503 + 5c8761f commit 3c15482
Show file tree
Hide file tree
Showing 64 changed files with 2,039 additions and 0 deletions.
31 changes: 31 additions & 0 deletions rustgym/desc/leetcode/d1/100.md
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>&nbsp;</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>&nbsp;</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> &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
</ul>
</div>
26 changes: 26 additions & 0 deletions rustgym/desc/leetcode/d1/101.md
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>&nbsp;</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>&nbsp;</p>

<p><b>Follow up:</b> Solve it both recursively and iteratively.</p>
</div>
21 changes: 21 additions & 0 deletions rustgym/desc/leetcode/d1/102.md
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>
21 changes: 21 additions & 0 deletions rustgym/desc/leetcode/d1/103.md
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>
36 changes: 36 additions & 0 deletions rustgym/desc/leetcode/d1/104.md
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>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p>

<p>&nbsp;</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>&nbsp;</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 &lt;= Node.val &lt;= 100</code></li>
</ul></div>
18 changes: 18 additions & 0 deletions rustgym/desc/leetcode/d1/105.md
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 =&nbsp;[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>
19 changes: 19 additions & 0 deletions rustgym/desc/leetcode/d1/106.md
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 =&nbsp;[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>
21 changes: 21 additions & 0 deletions rustgym/desc/leetcode/d1/107.md
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>
17 changes: 17 additions & 0 deletions rustgym/desc/leetcode/d1/108.md
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>
38 changes: 38 additions & 0 deletions rustgym/desc/leetcode/d1/109.md
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>&nbsp;</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>&nbsp;</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 &lt;= Node.val &lt;= 10^5</code></li>
</ul>
</div>
34 changes: 34 additions & 0 deletions rustgym/desc/leetcode/d1/110.md
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>&nbsp;</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>&nbsp;</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> &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
</ul></div>
27 changes: 27 additions & 0 deletions rustgym/desc/leetcode/d1/111.md
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>&nbsp;A leaf is a node with no children.</p>

<p>&nbsp;</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>&nbsp;</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 &lt;= Node.val &lt;= 1000</code></li>
</ul>
</div>
32 changes: 32 additions & 0 deletions rustgym/desc/leetcode/d1/112.md
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>&nbsp;</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>&nbsp;</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 &lt;= Node.val &lt;= 1000</code></li>
<li><code>-1000 &lt;= targetSum &lt;= 1000</code></li>
</ul>
</div>
Loading

0 comments on commit 3c15482

Please sign in to comment.