Skip to content

Commit ad4abce

Browse files
author
Shuo
committed
Add: Minimum Depth of Binary Tree
1 parent 04c80dd commit ad4abce

File tree

14 files changed

+145
-14
lines changed

14 files changed

+145
-14
lines changed

problems/3sum/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,19 @@ A solution set is:
3838
1. [3Sum Closest](../3sum-closest) (Medium)
3939
1. [4Sum](../4sum) (Medium)
4040
1. [3Sum Smaller](../3sum-smaller) (Medium)
41+
42+
### Hints
43+
<details>
44+
<summary>Hint 1</summary>
45+
So, we essentially need to find three numbers x, y, and z such that they add up to the given value. If we fix one of the numbers say x, we are left with the two-sum problem at hand!
46+
</details>
47+
48+
<details>
49+
<summary>Hint 2</summary>
50+
For the two-sum problem, if we fix one of the numbers, say <pre>x</pre>, we have to scan the entire array to find the next number<pre>y</pre> which is <pre>value - x</pre> where value is the input parameter. Can we change our array somehow so that this search becomes faster?
51+
</details>
52+
53+
<details>
54+
<summary>Hint 3</summary>
55+
The second train of thought for two-sum is, without changing the array, can we use additional space somehow? Like maybe a hash map to speed up the search?
56+
</details>

problems/bulb-switcher/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ So you should return 1, because there is only one bulb is on.
2828
</pre>
2929

3030
### Related Topics
31-
[[Brainteaser](../../tag/brainteaser/README.md)]
3231
[[Math](../../tag/math/README.md)]
32+
[[Brainteaser](../../tag/brainteaser/README.md)]
3333

3434
### Similar Questions
3535
1. [Bulb Switcher II](../bulb-switcher-ii) (Medium)

problems/container-with-most-water/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,26 @@
3535

3636
### Similar Questions
3737
1. [Trapping Rain Water](../trapping-rain-water) (Hard)
38+
39+
### Hints
40+
<details>
41+
<summary>Hint 1</summary>
42+
The aim is to maximize the area formed between the vertical lines. The area of any container is calculated using the shorter line as length and the distance between the lines as the width of the rectangle.
43+
44+
<pre>
45+
Area = length of shorter vertical line * distance between lines
46+
</pre>
47+
48+
We can definitely get the maximum width container as the outermost lines have the maximum distance between them. However, this container <b>might not be the maximum in size</b> as one of the vertical lines of this container could be really short.
49+
50+
<br>
51+
<img src="https://assets.leetcode.com/uploads/2019/10/20/hint_water_trap_1.png" width="500"/>
52+
53+
<br>
54+
<img src="https://assets.leetcode.com/uploads/2019/10/20/hint_water_trap_2.png" width="500"/>
55+
</details>
56+
57+
<details>
58+
<summary>Hint 2</summary>
59+
Start with the maximum width container and go to a shorter width container if there is a vertical line longer than the current containers shorter line. This way we are compromising on the width but we are looking forward to a longer length container.
60+
</details>

problems/count-and-say/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<code>11</code> is read off as <code>&quot;two 1s&quot;</code> or <code>21</code>.<br />
2626
<code>21</code> is read off as <code>&quot;one 2</code>, then <code>one 1&quot;</code> or <code>1211</code>.</p>
2727

28-
<p>Given an integer <i>n</i>&nbsp;where 1 &le; <em>n</em> &le; 30, generate the <i>n</i><sup>th</sup> term of the count-and-say sequence.</p>
28+
<p>Given an integer <i>n</i>&nbsp;where 1 &le; <em>n</em> &le; 30, generate the <i>n</i><sup>th</sup> term of the count-and-say sequence. You can do so recursively, in other words from the previous member&nbsp;read off the digits, counting the number of digits in groups of the same digit.</p>
2929

3030
<p>Note: Each term of the sequence of integers will be represented as a string.</p>
3131

@@ -36,13 +36,16 @@
3636
<pre>
3737
<b>Input:</b> 1
3838
<b>Output:</b> &quot;1&quot;
39+
<b>Explanation:</b> This is the base case.
3940
</pre>
4041

4142
<p><b>Example 2:</b></p>
4243

4344
<pre>
4445
<b>Input:</b> 4
45-
<b>Output:</b> &quot;1211&quot;</pre>
46+
<b>Output:</b> &quot;1211&quot;
47+
<b>Explanation:</b> For n = 3 the term was &quot;21&quot; in which we have two groups &quot;2&quot; and &quot;1&quot;, &quot;2&quot; can be read as &quot;12&quot; which means frequency = 1 and value = 2, the same way &quot;1&quot; is read as &quot;11&quot;, so the answer is the concatenation of &quot;12&quot; and &quot;11&quot; which is &quot;1211&quot;.
48+
</pre>
4649

4750
### Related Topics
4851
[[String](../../tag/string/README.md)]

problems/count-of-smaller-numbers-after-self/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ To the right of 1 there is <b>0</b> smaller element.
2626
</pre>
2727

2828
### Related Topics
29+
[[Binary Search](../../tag/binary-search/README.md)]
30+
[[Divide and Conquer](../../tag/divide-and-conquer/README.md)]
2931
[[Sort](../../tag/sort/README.md)]
3032
[[Binary Indexed Tree](../../tag/binary-indexed-tree/README.md)]
3133
[[Segment Tree](../../tag/segment-tree/README.md)]
32-
[[Binary Search](../../tag/binary-search/README.md)]
33-
[[Divide and Conquer](../../tag/divide-and-conquer/README.md)]
3434

3535
### Similar Questions
3636
1. [Count of Range Sum](../count-of-range-sum) (Hard)

problems/create-maximum-number/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ k = <code>3</code>
4747
</pre>
4848

4949
### Related Topics
50-
[[Greedy](../../tag/greedy/README.md)]
5150
[[Dynamic Programming](../../tag/dynamic-programming/README.md)]
51+
[[Greedy](../../tag/greedy/README.md)]
5252

5353
### Similar Questions
5454
1. [Remove K Digits](../remove-k-digits) (Medium)

problems/generalized-abbreviation/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
<p>&nbsp;</p>
2727

2828
### Related Topics
29-
[[Bit Manipulation](../../tag/bit-manipulation/README.md)]
3029
[[Backtracking](../../tag/backtracking/README.md)]
30+
[[Bit Manipulation](../../tag/bit-manipulation/README.md)]
3131

3232
### Similar Questions
3333
1. [Subsets](../subsets) (Medium)

problems/length-of-last-word/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
## [58. Length of Last Word (Easy)](https://leetcode.com/problems/length-of-last-word "最后一个单词的长度")
1313

14-
<p>Given a string <i>s</i> consists of upper/lower-case alphabets and empty space characters <code>&#39; &#39;</code>, return the length of last word in the string.</p>
14+
<p>Given a string <i>s</i> consists of upper/lower-case alphabets and empty space characters <code>&#39; &#39;</code>, return the length of last word (last word means the last appearing word if we loop from left to right) in the string.</p>
1515

1616
<p>If the last word does not exist, return 0.</p>
1717

18-
<p><b>Note:</b> A word is defined as a character sequence consists of non-space characters only.</p>
18+
<p><b>Note:</b> A word is defined as a <strong>maximal substring</strong> consisting&nbsp;of non-space characters only.</p>
1919

2020
<p><b>Example:</b></p>
2121

problems/merge-sorted-array/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,10 @@ nums2 = [2,5,6], n = 3
4242
### Hints
4343
<details>
4444
<summary>Hint 1</summary>
45-
What if you fill the longer array from the end instead of start ?
45+
You can easily solve this problem if you simply think about two elements at a time rather than two arrays. We know that each of the individual arrays is sorted. What we don't know is how they will intertwine. Can we take a local decision and arrive at an optimal solution?
46+
</details>
47+
48+
<details>
49+
<summary>Hint 2</summary>
50+
If you simply consider one element each at a time from the two arrays and make a decision and proceed accordingly, you will arrive at the optimal solution.
4651
</details>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,29 @@
11
package problem111
2+
3+
import "github.com/openset/leetcode/internal/kit"
4+
5+
// TreeNode - Definition for a binary tree node.
6+
type TreeNode = kit.TreeNode
7+
8+
/**
9+
* Definition for a binary tree node.
10+
* type TreeNode struct {
11+
* Val int
12+
* Left *TreeNode
13+
* Right *TreeNode
14+
* }
15+
*/
16+
func minDepth(root *TreeNode) int {
17+
if root == nil {
18+
return 0
19+
}
20+
l := minDepth(root.Left)
21+
r := minDepth(root.Right)
22+
if l == 0 || r == 0 {
23+
return l + r + 1
24+
}
25+
if l < r {
26+
return l + 1
27+
}
28+
return r + 1
29+
}

0 commit comments

Comments
 (0)