vault backup: 2022-07-05 09:44:58

This commit is contained in:
juan 2022-07-05 09:44:58 +08:00
parent 9cfd683feb
commit e62c97e039
5 changed files with 101 additions and 7 deletions

View file

@ -32,6 +32,6 @@
"repelStrength": 10, "repelStrength": 10,
"linkStrength": 1, "linkStrength": 1,
"linkDistance": 250, "linkDistance": 250,
"scale": 0.9752800867986091, "scale": 0.975280086798608,
"close": true "close": true
} }

View file

@ -20,6 +20,8 @@ tag:#CS_list_need_understanding
- [ ] [[$filename]] - [ ] [[$filename]]
``` ```
- [ ] [[Leetcode Binary-Tree-Preorder-Traversal]]
- [ ] [[Leetcode Implement-Queue-Using-Stacks]]
- [ ] [[Leetcode Reverse-Linked-List]] - [ ] [[Leetcode Reverse-Linked-List]]
@ -30,6 +32,7 @@ tag:#CS_list_need_practicing
- [ ] [[$filename]] - [ ] [[$filename]]
``` ```
- [ ] [[Leetcode Binary-Tree-Postorder-Traversal]]
- [ ] [[Leetcode Reverse-Linked-List]] - [ ] [[Leetcode Reverse-Linked-List]]
@ -45,9 +48,16 @@ tag:#leetcode
- [[Leetcode Valid-Parentheses]] - [[Leetcode Valid-Parentheses]]
- [[Leetcode Best-Time-To-Buy-And-Sell-Stock]] - [[Leetcode Best-Time-To-Buy-And-Sell-Stock]]
- [[Leetcode Binary-Tree-Inorder-Traversal]]
- [[Leetcode Binary-Tree-Level-Order-Traversal]]
- [[Leetcode Binary-Tree-Postorder-Traversal]]
- [[Leetcode Binary-Tree-Preorder-Traversal]]
- [[Leetcode First-Unique-Character-In-a-String]] - [[Leetcode First-Unique-Character-In-a-String]]
- [[Leetcode Implement-Queue-Using-Stacks]]
- [[Leetcode Intersection-of-Two-Arrays-II]] - [[Leetcode Intersection-of-Two-Arrays-II]]
- [[Leetcode Linked-List-Cycle]] - [[Leetcode Linked-List-Cycle]]
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
- [[Leetcode Maximum-Difference-Between-Increasing-Elements]]
- [[Leetcode Maxinum-subarray]] - [[Leetcode Maxinum-subarray]]
- [[Leetcode Merge-Sorted-Array]] - [[Leetcode Merge-Sorted-Array]]
- [[Leetcode Merge-Two-Sorted-Lists]] - [[Leetcode Merge-Two-Sorted-Lists]]
@ -86,9 +96,15 @@ tag:#DS tag:#coding_problem -tag:#template_remove_me
- [[Leetcode Valid-Parentheses]] - [[Leetcode Valid-Parentheses]]
- [[Leetcode Best-Time-To-Buy-And-Sell-Stock]] - [[Leetcode Best-Time-To-Buy-And-Sell-Stock]]
- [[Leetcode Binary-Tree-Inorder-Traversal]]
- [[Leetcode Binary-Tree-Level-Order-Traversal]]
- [[Leetcode Binary-Tree-Postorder-Traversal]]
- [[Leetcode Binary-Tree-Preorder-Traversal]]
- [[Leetcode First-Unique-Character-In-a-String]] - [[Leetcode First-Unique-Character-In-a-String]]
- [[Leetcode Implement-Queue-Using-Stacks]]
- [[Leetcode Intersection-of-Two-Arrays-II]] - [[Leetcode Intersection-of-Two-Arrays-II]]
- [[Leetcode Linked-List-Cycle]] - [[Leetcode Linked-List-Cycle]]
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
- [[Leetcode Maxinum-subarray]] - [[Leetcode Maxinum-subarray]]
- [[Leetcode Merge-Sorted-Array]] - [[Leetcode Merge-Sorted-Array]]
- [[Leetcode Merge-Two-Sorted-Lists]] - [[Leetcode Merge-Two-Sorted-Lists]]
@ -126,9 +142,15 @@ tag:#algorithm tag:#coding_problem -tag:#template_remove_me
``` ```
- [[Leetcode Best-Time-To-Buy-And-Sell-Stock]] - [[Leetcode Best-Time-To-Buy-And-Sell-Stock]]
- [[Leetcode Binary-Tree-Inorder-Traversal]]
- [[Leetcode Binary-Tree-Level-Order-Traversal]]
- [[Leetcode Binary-Tree-Postorder-Traversal]]
- [[Leetcode Binary-Tree-Preorder-Traversal]]
- [[Leetcode First-Unique-Character-In-a-String]] - [[Leetcode First-Unique-Character-In-a-String]]
- [[Leetcode Intersection-of-Two-Arrays-II]] - [[Leetcode Intersection-of-Two-Arrays-II]]
- [[Leetcode Linked-List-Cycle]] - [[Leetcode Linked-List-Cycle]]
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
- [[Leetcode Maximum-Difference-Between-Increasing-Elements]]
- [[Leetcode Maxinum-subarray]] - [[Leetcode Maxinum-subarray]]
- [[Leetcode Merge-Sorted-Array]] - [[Leetcode Merge-Sorted-Array]]
- [[Leetcode Merge-Two-Sorted-Lists]] - [[Leetcode Merge-Two-Sorted-Lists]]

View file

@ -31,13 +31,13 @@ Given the `root` of a binary tree, return _the level order traversal of its node
![](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg) ![](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg)
**Input:** root = [3,9,20,null,null,15,7] **Input:** root = `[3,9,20,null,null,15,7]`
**Output:** [[3],[9,20],[15,7]] **Output:** `[[3],[9,20],[15,7]]`
**Example 2:** **Example 2:**
**Input:** root = [1] **Input:** root = [1]
**Output:** [[1]] **Output:** `[[1]]`
**Example 3:** **Example 3:**
@ -58,7 +58,7 @@ In contrary to DFS, BFS uses queue. and there are many tricks for pushing 2d arr
vector<vector<int>> vec; vector<vector<int>> vec;
vec.push_back({}); vec.push_back({});
vec.back().push_back(5); vec.back().push_back(5);
// [[5]] // [[ 5 ]]
``` ```
### Solution ### Solution

View file

@ -38,8 +38,10 @@ Implement the `MyQueue` class:
#### Examples #### Examples
**Input** **Input**
```
["MyQueue", "push", "push", "peek", "pop", "empty"] ["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []] [[], [1], [2], [], [], []]
```
**Output** **Output**
[null, null, null, 1, 1, false] [null, null, null, 1, 1, false]

View file

@ -21,17 +21,87 @@ tag:#BFS
##### Links: ##### Links:
- [Link to problem]() - [Link to problem](https://leetcode.com/problems/maximum-depth-of-binary-tree/)
___ ___
### Problem ### Problem
Given the `root` of a binary tree, return _its maximum depth_.
A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node.
#### Examples #### Examples
**Example 1:**
![](https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg)
**Input:** root = [3,9,20,null,null,15,7]
**Output:** 3
**Example 2:**
**Input:** root = [1,null,2]
**Output:** 2
#### Constraints #### Constraints
- The number of nodes in the tree is in the range `[0, 104]`.
- `-100 <= Node.val <= 100`
### Thoughts ### Thoughts
> [!summary] > [!summary]
> This is a #template_remove_me > This problem can be solved by #BFS or #DFS
BFS way:
Simply log the level in each while iteration.
DFS way: (Popular)
Use recursion
### Solution ### Solution
DFS Recursion:
```cpp
```
BFS:
```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
* right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode *root) {
// BFS
int levels = 0;
queue<TreeNode *> pending;
TreeNode *ptr = root;
if (ptr)
pending.push(ptr);
while (!pending.empty()) {
levels++;
for (int i = 0, size = pending.size(); i < size; i++) {
ptr = pending.front();
pending.pop();
if (ptr->left)
pending.push(ptr->left);
if (ptr->right)
pending.push(ptr->right);
}
}
return levels;
}
};
```