Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add: Minimum Depth of Binary Tree #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
awesee merged 1 commit into master from develop
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions problems/3sum/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,19 @@ A solution set is:
1. [3Sum Closest](../3sum-closest) (Medium)
1. [4Sum](../4sum) (Medium)
1. [3Sum Smaller](../3sum-smaller) (Medium)

### Hints
<details>
<summary>Hint 1</summary>
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!
</details>

<details>
<summary>Hint 2</summary>
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?
</details>

<details>
<summary>Hint 3</summary>
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?
</details>
2 changes: 1 addition & 1 deletion problems/bulb-switcher/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ So you should return 1, because there is only one bulb is on.
</pre>

### Related Topics
[[Brainteaser](../../tag/brainteaser/README.md)]
[[Math](../../tag/math/README.md)]
[[Brainteaser](../../tag/brainteaser/README.md)]

### Similar Questions
1. [Bulb Switcher II](../bulb-switcher-ii) (Medium)
Expand Down
23 changes: 23 additions & 0 deletions problems/container-with-most-water/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,26 @@

### Similar Questions
1. [Trapping Rain Water](../trapping-rain-water) (Hard)

### Hints
<details>
<summary>Hint 1</summary>
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.

<pre>
Area = length of shorter vertical line * distance between lines
</pre>

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.

<br>
<img src="https://assets.leetcode.com/uploads/2019/10/20/hint_water_trap_1.png" width="500"/>

<br>
<img src="https://assets.leetcode.com/uploads/2019/10/20/hint_water_trap_2.png" width="500"/>
</details>

<details>
<summary>Hint 2</summary>
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.
</details>
7 changes: 5 additions & 2 deletions problems/count-and-say/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<code>11</code> is read off as <code>&quot;two 1s&quot;</code> or <code>21</code>.<br />
<code>21</code> is read off as <code>&quot;one 2</code>, then <code>one 1&quot;</code> or <code>1211</code>.</p>

<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>
<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>

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

Expand All @@ -36,13 +36,16 @@
<pre>
<b>Input:</b> 1
<b>Output:</b> &quot;1&quot;
<b>Explanation:</b> This is the base case.
</pre>

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

<pre>
<b>Input:</b> 4
<b>Output:</b> &quot;1211&quot;</pre>
<b>Output:</b> &quot;1211&quot;
<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;.
</pre>

### Related Topics
[[String](../../tag/string/README.md)]
Expand Down
4 changes: 2 additions & 2 deletions problems/count-of-smaller-numbers-after-self/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ To the right of 1 there is <b>0</b> smaller element.
</pre>

### Related Topics
[[Binary Search](../../tag/binary-search/README.md)]
[[Divide and Conquer](../../tag/divide-and-conquer/README.md)]
[[Sort](../../tag/sort/README.md)]
[[Binary Indexed Tree](../../tag/binary-indexed-tree/README.md)]
[[Segment Tree](../../tag/segment-tree/README.md)]
[[Binary Search](../../tag/binary-search/README.md)]
[[Divide and Conquer](../../tag/divide-and-conquer/README.md)]

### Similar Questions
1. [Count of Range Sum](../count-of-range-sum) (Hard)
Expand Down
2 changes: 1 addition & 1 deletion problems/create-maximum-number/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ k = <code>3</code>
</pre>

### Related Topics
[[Greedy](../../tag/greedy/README.md)]
[[Dynamic Programming](../../tag/dynamic-programming/README.md)]
[[Greedy](../../tag/greedy/README.md)]

### Similar Questions
1. [Remove K Digits](../remove-k-digits) (Medium)
Expand Down
2 changes: 1 addition & 1 deletion problems/generalized-abbreviation/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
<p>&nbsp;</p>

### Related Topics
[[Bit Manipulation](../../tag/bit-manipulation/README.md)]
[[Backtracking](../../tag/backtracking/README.md)]
[[Bit Manipulation](../../tag/bit-manipulation/README.md)]

### Similar Questions
1. [Subsets](../subsets) (Medium)
Expand Down
4 changes: 2 additions & 2 deletions problems/length-of-last-word/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

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

<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>
<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>

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

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

<p><b>Example:</b></p>

Expand Down
7 changes: 6 additions & 1 deletion problems/merge-sorted-array/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@ nums2 = [2,5,6], n = 3
### Hints
<details>
<summary>Hint 1</summary>
What if you fill the longer array from the end instead of start ?
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?
</details>

<details>
<summary>Hint 2</summary>
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.
</details>
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
package problem111

import "github.com/openset/leetcode/internal/kit"

// TreeNode - Definition for a binary tree node.
type TreeNode = kit.TreeNode

/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func minDepth(root *TreeNode) int {
if root == nil {
return 0
}
l := minDepth(root.Left)
r := minDepth(root.Right)
if l == 0 || r == 0 {
return l + r + 1
}
if l < r {
return l + 1
}
return r + 1
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
package problem111

import (
"testing"

"github.com/openset/leetcode/internal/kit"
)

type testType struct {
in []int
want int
}

func TestMinDepth(t *testing.T) {
tests := [...]testType{
{
in: []int{3, 9, 20, kit.NULL, kit.NULL, 15, 7},
want: 2,
},
{
in: []int{1, 2},
want: 2,
},
{
in: nil,
want: 0,
},
}
for _, tt := range tests {
got := minDepth(kit.SliceInt2TreeNode(tt.in))
if got != tt.want {
t.Fatalf("in: %v, got: %v, want: %v", tt.in, got, tt.want)
}
}
}
19 changes: 19 additions & 0 deletions problems/remove-duplicates-from-sorted-array/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,22 @@ for (int i = 0; i &lt; len; i++) {
### Similar Questions
1. [Remove Element](../remove-element) (Easy)
1. [Remove Duplicates from Sorted Array II](../remove-duplicates-from-sorted-array-ii) (Medium)

### Hints
<details>
<summary>Hint 1</summary>
In this problem, the key point to focus on is the input array being sorted. As far as duplicate elements are concerned, what is their positioning in the array when the given array is sorted? Look at the image above for the answer. If we know the position of one of the elements, do we also know the positioning of all the duplicate elements?

<br>
<img src="https://assets.leetcode.com/uploads/2019/10/20/hint_rem_dup.png" width="500"/>
</details>

<details>
<summary>Hint 2</summary>
We need to modify the array in-place and the size of the final array would potentially be smaller than the size of the input array. So, we ought to use a two-pointer approach here. One, that would keep track of the current element in the original array and another one for just the unique elements.
</details>

<details>
<summary>Hint 3</summary>
Essentially, once an element is encountered, you simply need to <b>bypass</b> its duplicates and move on to the next unique element.
</details>
7 changes: 4 additions & 3 deletions problems/remove-element/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,16 @@ for (int i = 0; i &lt; len; i++) {
### Hints
<details>
<summary>Hint 1</summary>
Try two pointers.
The problem statement clearly asks us to modify the array in-place and it also says that the element beyond the new length of the array can be anything. Given an element, we need to remove all the occurrences of it from the array. We don't technically need to <b>remove</b> that element per-say, right?
</details>

<details>
<summary>Hint 2</summary>
Did you use the property of "the order of elements can be changed"?
We can move all the occurrences of this element to the end of the array. Use two pointers!
<br><img src="https://assets.leetcode.com/uploads/2019/10/20/hint_remove_element.png" width="500"/>
</details>

<details>
<summary>Hint 3</summary>
What happens when the elements to remove are rare?
Yet another direction of thought is to consider the elements to be removed as non-existent. In a single pass, if we keep copying the visible elements in-place, that should also solve this problem for us.
</details>
4 changes: 3 additions & 1 deletion problems/substring-with-concatenation-of-all-words/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@

<p>You are given a string, <strong>s</strong>, and a list of words, <strong>words</strong>, that are all of the same length. Find all starting indices of substring(s) in <strong>s</strong> that is a concatenation of each word in <strong>words</strong> exactly once and without any intervening characters.</p>

<p>&nbsp;</p>

<p><strong>Example 1:</strong></p>

<pre>
<strong>Input:
s =</strong> &quot;barfoothefoobarman&quot;,
<strong> words = </strong>[&quot;foo&quot;,&quot;bar&quot;]
<strong>Output:</strong> <code>[0,9]</code>
<strong>Explanation:</strong> Substrings starting at index 0 and 9 are &quot;barfoor&quot; and &quot;foobar&quot; respectively.
<strong>Explanation:</strong> Substrings starting at index 0 and 9 are &quot;barfoo&quot; and &quot;foobar&quot; respectively.
The output order does not matter, returning [9,0] is fine too.
</pre>

Expand Down

AltStyle によって変換されたページ (->オリジナル) /