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

Commit 73eeb82

Browse files
updating docs
1 parent 17eb93f commit 73eeb82

File tree

6 files changed

+328
-4
lines changed

6 files changed

+328
-4
lines changed

‎docs/.vitepress/Sidebar.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,14 @@ export default Sidebar = [
262262
text: "222 - Count Complete Tree Nodes",
263263
link: "/solution/0201-0300/222 - Count Complete Tree Nodes.md",
264264
},
265+
{
266+
text: "225 - Implement Stack using Queues",
267+
link: "/solution/0201-0300/225 - Implement Stack using Queues.md",
268+
},
269+
{
270+
text: "226 - Invert Binary Tree",
271+
link: "/solution/0201-0300/226 - Invert Binary Tree.md",
272+
},
265273
{
266274
text: "230 - Kth Smallest Element in a BST",
267275
link: "/solution/0201-0300/230 - Kth Smallest Element in a BST.md",
@@ -270,6 +278,10 @@ export default Sidebar = [
270278
text: "231 - Power of Two",
271279
link: "/solution/0201-0300/231 - Power of Two.md",
272280
},
281+
{
282+
text: "232 - Implement Queue using Stacks",
283+
link: "/solution/0201-0300/232 - Implement Queue using Stacks.md",
284+
},
273285
{
274286
text: "236 - Lowest Common Ancestor of a Binary Tree",
275287
link: "/solution/0201-0300/236 - Lowest Common Ancestor of a Binary Tree.md",

‎docs/SERIALWISE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,11 @@
7575
| **0217** | [Contains Duplicate][217] | Array, Hash Table, Sorting | ![][easy] | |
7676
| **0219** | [Contains Duplicate II][219] | Array, Hash Table, Sliding Window | ![][easy] | |
7777
| **0222** | [Count Complete Tree Nodes][222] | Binary Search, Tree, Depth First Search, Binary Tree | ![][medium] | |
78+
| **0225** | [Implement Stack using Queues][225] | Stack, Design, Q | ![][easy] | |
79+
| **0226** | [Invert Binary Tree][226] | Tree, Depth-First Search, Breadth-First Search, Binary Tree | ![][easy] | |
7880
| **0230** | [Kth Smallest Element in a BST][230] | Tree, Depth First Search, Binary Search Tree, Binary Tree | ![][medium] | |
7981
| **0231** | [Power of Two][231] | Math, Bit Manipulation, Recursion | ![][easy] | |
82+
| **0232** | [Implement Queue using Stacks][232] | Stack, Design, Q | ![][easy] | |
8083
| **0236** | [Lowest Common Ancestor of a Binary Tree][236] | Tree, Depth-First Search, Binary Tree | ![][medium] | |
8184
| **0278** | [First Bad Version][278] | Binary Search, Interactive | ![][easy] | |
8285
| **0283** | [Move Zeroes][283] | Array, Two Pointers | ![][easy] | |
@@ -190,8 +193,11 @@
190193
[217]: ./solution/0201-0300/217%20-%20Contains%20Duplicate.md
191194
[219]: ./solution/0201-0300/219%20-%20Contains%20Duplicate%20II.md
192195
[222]: ./solution/0201-0300/222%20-%20Count%20Complete%20Tree%20Nodes.md
196+
[225]: ./solution/0201-0300/225%20-%20Implement%20Stack%20using%20Queues.md
197+
[226]: ./solution/0201-0300/226%20-%20Invert%20Binary%20Tree.md
193198
[230]: ./solution/0201-0300/230%20-%20Kth%20Smallest%20Element%20in%20a%20BST.md
194199
[231]: ./solution/0201-0300/231%20-%20Power%20of%20Two.md
200+
[232]: ./solution/0201-0300/232%20-%20Implement%20Queue%20using%20Stacks.md
195201
[236]: ./solution/0201-0300/236%20-%20Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree.md
196202
[239]: ./solution/0201-0300/239%20-%20Sliding%20Window%20Maximum.md
197203
[278]: ./solution/0201-0300/278%20-%20First%20Bad%20Version.md

‎docs/TOPICWISE.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@
243243
| **0145** | [Binary Tree Postorder Traversal][145] | Stack, Tree, Depth First Search, Binary Tree | ![][easy] | |
244244
| **0199** | [Binary Tree Right Side View][199] | Tree, Depth First Search, Breadth First Search, Binary Tree | ![][medium] | |
245245
| **0222** | [Count Complete Tree Nodes][222] | Binary Search, Tree, Depth First Search, Binary Tree | ![][medium] | |
246+
| **0226** | [Invert Binary Tree][226] | Tree, Depth-First Search, Breadth-First Search, Binary Tree | ![][easy] | |
246247
| **0230** | [Kth Smallest Element in a BST ][230] | Tree, Depth First Search, Binary Search Tree, Binary Tree | ![][medium] | |
247248
| **0236** | [Lowest Common Ancestor of a Binary Tree][236] | Tree, Depth-First Search, Binary Tree | ![][medium] | |
248249
| **0337** | [House Robber III][337] | Dynamic Programming, Tree, Depth First Search, Binary Tree | ![][medium] | |
@@ -298,6 +299,7 @@
298299
| **0145** | [Binary Tree Postorder Traversal][145] | Stack, Tree, Depth First Search, Binary Tree | ![][easy] | |
299300
| **0199** | [Binary Tree Right Side View][199] | Tree, Depth First Search, Breadth First Search, Binary Tree | ![][medium] | |
300301
| **0222** | [Count Complete Tree Nodes][222] | Binary Search, Tree, Depth First Search, Binary Tree | ![][medium] | |
302+
| **0226** | [Invert Binary Tree][226] | Tree, Depth-First Search, Breadth-First Search, Binary Tree | ![][easy] | |
301303
| **0230** | [Kth Smallest Element in a BST ][230] | Tree, Depth First Search, Binary Search Tree, Binary Tree | ![][medium] | |
302304
| **0236** | [Lowest Common Ancestor of a Binary Tree][236] | Tree, Depth-First Search, Binary Tree | ![][medium] | |
303305
| **0337** | [House Robber III][337] | Dynamic Programming, Tree, Depth First Search, Binary Tree | ![][medium] | |
@@ -354,6 +356,8 @@
354356
| **0094** | [Binary Tree Inorder Traversal][94] | Tree, Stack, Depth First Search, Binary Tree | ![][easy] | |
355357
| **0144** | [Binary Tree Preorder Traversal][144] | Stack, Tree, Depth First Search, Binary Tree | ![][easy] | |
356358
| **0145** | [Binary Tree Postorder Traversal][145] | Stack, Tree, Depth First Search, Binary Tree | ![][easy] | |
359+
| **0225** | [Implement Stack using Queues][225] | Stack, Design, Q | ![][easy] | |
360+
| **0232** | [Implement Queue using Stacks][232] | Stack, Design, Q | ![][easy] | |
357361
| **0445** | [Add Two Numbers II][445] | Linked List, Math, Stack | ![][medium] | |
358362
| **0739** | [Daily Temperatures][739] | Array, Stack, Monotonic Stack | ![][medium] | |
359363
| **1047** | [Remove All Adjacent Duplicates In String][1047] | String, Stack | ![][easy] | |
@@ -362,8 +366,10 @@
362366

363367
## Design
364368

365-
| # | Solution | Tags | Difficulty | Remark |
366-
| :-: | :------: | :--: | :--------: | :----: |
369+
| # | Solution | Tags | Difficulty | Remark |
370+
| :------: | :---------------------------------: | :--------------: | :--------: | :----: |
371+
| **0225** | [Implement Stack using Queues][225] | Stack, Design, Q | ![][easy] | |
372+
| **0232** | [Implement Queue using Stacks][232] | Stack, Design, Q | ![][easy] | |
367373

368374
<!---------------------------------{ Solution Table }-------------------------->
369375

@@ -530,8 +536,10 @@
530536

531537
## Queue
532538

533-
| # | Solution | Tags | Difficulty | Remark |
534-
| :-: | :------: | :--: | :--------: | :----: |
539+
| # | Solution | Tags | Difficulty | Remark |
540+
| :------: | :---------------------------------: | :--------------: | :--------: | :----: |
541+
| **0225** | [Implement Stack using Queues][225] | Stack, Design, Q | ![][easy] | |
542+
| **0232** | [Implement Queue using Stacks][232] | Stack, Design, Q | ![][easy] | |
535543

536544
<!---------------------------------{ Solution Table }-------------------------->
537545

@@ -857,8 +865,11 @@
857865
[217]: ./solution/0201-0300/217%20-%20Contains%20Duplicate.md
858866
[219]: ./solution/0201-0300/219%20-%20Contains%20Duplicate%20II.md
859867
[222]: ./solution/0201-0300/222%20-%20Count%20Complete%20Tree%20Nodes.md
868+
[225]: ./solution/0201-0300/225%20-%20Implement%20Stack%20using%20Queues.md
869+
[226]: ./solution/0201-0300/226%20-%20Invert%20Binary%20Tree.md
860870
[230]: ./solution/0201-0300/230%20-%20Kth%20Smallest%20Element%20in%20a%20BST.md
861871
[231]: ./solution/0201-0300/231%20-%20Power%20of%20Two.md
872+
[232]: ./solution/0201-0300/232%20-%20Implement%20Queue%20using%20Stacks.md
862873
[236]: ./solution/0201-0300/236%20-%20Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree.md
863874
[239]: ./solution/0201-0300/239%20-%20Sliding%20Window%20Maximum.md
864875
[278]: ./solution/0201-0300/278%20-%20First%20Bad%20Version.md
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# 225. Implement Stack using Queues [![share]](https://leetcode.com/problems/implement-stack-using-queues/)
2+
3+
![][easy]
4+
5+
## Problem Statement
6+
7+
<p>Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (<code>push</code>, <code>top</code>, <code>pop</code>, and <code>empty</code>).</p>
8+
<p>Implement the <code>MyStack</code> class:</p>
9+
<ul>
10+
<li><code>void push(int x)</code> Pushes element x to the top of the stack.</li>
11+
<li><code>int pop()</code> Removes the element on the top of the stack and returns it.</li>
12+
<li><code>int top()</code> Returns the element on the top of the stack.</li>
13+
<li><code>boolean empty()</code> Returns <code>true</code> if the stack is empty, <code>false</code> otherwise.</li>
14+
</ul>
15+
<p><b>Notes:</b></p>
16+
<ul>
17+
<li>You must use <strong>only</strong> standard operations of a queue, which means that only <code>push to back</code>, <code>peek/pop from front</code>, <code>size</code> and <code>is empty</code> operations are valid.</li>
18+
<li>Depending on your language, the queue may not be supported natively. You may simulate a queue using a list or deque (double-ended queue) as long as you use only a queue's standard operations.</li>
19+
</ul>
20+
<p> </p>
21+
<p><strong class="example">Example 1:</strong></p>
22+
23+
```
24+
Input
25+
["MyStack", "push", "push", "top", "pop", "empty"]
26+
[[], [1], [2], [], [], []]
27+
Output
28+
[null, null, null, 2, 2, false]
29+
30+
Explanation
31+
MyStack myStack = new MyStack();
32+
myStack.push(1);
33+
myStack.push(2);
34+
myStack.top(); // return 2
35+
myStack.pop(); // return 2
36+
myStack.empty(); // return False
37+
```
38+
39+
<p> </p>
40+
<p><strong>Constraints:</strong></p>
41+
<ul>
42+
<li><code>1 &lt;= x &lt;= 9</code></li>
43+
<li>At most <code>100</code> calls will be made to <code>push</code>, <code>pop</code>, <code>top</code>, and <code>empty</code>.</li>
44+
<li>All the calls to <code>pop</code> and <code>top</code> are valid.</li>
45+
</ul>
46+
<p> </p>
47+
<p><strong>Follow-up:</strong> Can you implement the stack using only one queue?</p>
48+
49+
## Solution:
50+
51+
::: code-group
52+
53+
```go [Go]
54+
package main
55+
56+
type MyStack struct {
57+
queue []int
58+
}
59+
60+
func Constructor() MyStack {
61+
return MyStack{}
62+
}
63+
64+
// Push x onto stack.
65+
func (this *MyStack) Push(x int) {
66+
this.queue = append(this.queue, x)
67+
}
68+
69+
// Removes and returns the element on top of the stack
70+
func (this *MyStack) Pop() int {
71+
if len(this.queue) == 0 {
72+
return 0
73+
}
74+
pop := this.queue[len(this.queue)-1]
75+
this.queue = this.queue[:len(this.queue)-1]
76+
return pop
77+
}
78+
79+
// Get the top element.
80+
func (this *MyStack) Top() int {
81+
if len(this.queue) == 0 {
82+
return 0
83+
}
84+
return this.queue[len(this.queue)-1]
85+
}
86+
87+
// Return whether the stack is empty.
88+
func (this *MyStack) Empty() bool {
89+
return len(this.queue) == 0
90+
}
91+
92+
```
93+
94+
:::
95+
96+
### [_..._](#)
97+
98+
```
99+
100+
```
101+
102+
<!----------------------------------{ link }--------------------------------->
103+
104+
[share]: https://graph.org/file/3ea5234dda646b71c574a.png
105+
[easy]: https://img.shields.io/badge/Difficulty-Easy-bright.svg
106+
[medium]: https://img.shields.io/badge/Difficulty-Medium-yellow.svg
107+
[hard]: https://img.shields.io/badge/Difficulty-Hard-red.svg
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# 226. Invert Binary Tree [![share]](https://leetcode.com/problems/invert-binary-tree/)
2+
3+
![][easy]
4+
5+
## Problem Statement
6+
7+
<p>Given the <code>root</code> of a binary tree, invert the tree, and return <em>its root</em>.</p>
8+
<p> </p>
9+
<p><strong class="example">Example 1:</strong></p>
10+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg" style="width: 500px; height: 165px;"/>
11+
12+
```
13+
Input: root = [4,2,7,1,3,6,9]
14+
Output: [4,7,2,9,6,3,1]
15+
```
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg" style="width: 500px; height: 120px;"/>
19+
20+
```
21+
Input: root = [2,1,3]
22+
Output: [2,3,1]
23+
```
24+
25+
<p><strong class="example">Example 3:</strong></p>
26+
27+
```
28+
Input: root = []
29+
Output: []
30+
```
31+
32+
<p> </p>
33+
<p><strong>Constraints:</strong></p>
34+
<ul>
35+
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
36+
<li><code>-100 &lt;= Node.val &lt;= 100</code></li>
37+
</ul>
38+
39+
## Solution:
40+
41+
::: code-group
42+
43+
```go [Go]
44+
package main
45+
46+
// Definition for a binary tree node.
47+
type TreeNode struct {
48+
Val int
49+
Left *TreeNode
50+
Right *TreeNode
51+
}
52+
53+
func invertTree(root *TreeNode) *TreeNode {
54+
if root == nil {
55+
return nil
56+
}
57+
58+
invertTree(root.Left)
59+
invertTree(root.Right)
60+
61+
root.Left, root.Right = root.Right, root.Left
62+
63+
return root
64+
}
65+
66+
```
67+
68+
:::
69+
70+
### [_..._](#)
71+
72+
```
73+
74+
```
75+
76+
<!----------------------------------{ link }--------------------------------->
77+
78+
[share]: https://graph.org/file/3ea5234dda646b71c574a.png
79+
[easy]: https://img.shields.io/badge/Difficulty-Easy-bright.svg
80+
[medium]: https://img.shields.io/badge/Difficulty-Medium-yellow.svg
81+
[hard]: https://img.shields.io/badge/Difficulty-Hard-red.svg

0 commit comments

Comments
(0)

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