vault backup: 2022-07-05 09:44:58
This commit is contained in:
parent
9cfd683feb
commit
e62c97e039
2
.obsidian/graph.json
vendored
2
.obsidian/graph.json
vendored
|
@ -32,6 +32,6 @@
|
|||
"repelStrength": 10,
|
||||
"linkStrength": 1,
|
||||
"linkDistance": 250,
|
||||
"scale": 0.9752800867986091,
|
||||
"scale": 0.975280086798608,
|
||||
"close": true
|
||||
}
|
|
@ -20,6 +20,8 @@ tag:#CS_list_need_understanding
|
|||
- [ ] [[$filename]]
|
||||
```
|
||||
|
||||
- [ ] [[Leetcode Binary-Tree-Preorder-Traversal]]
|
||||
- [ ] [[Leetcode Implement-Queue-Using-Stacks]]
|
||||
- [ ] [[Leetcode Reverse-Linked-List]]
|
||||
|
||||
|
||||
|
@ -30,6 +32,7 @@ tag:#CS_list_need_practicing
|
|||
- [ ] [[$filename]]
|
||||
```
|
||||
|
||||
- [ ] [[Leetcode Binary-Tree-Postorder-Traversal]]
|
||||
- [ ] [[Leetcode Reverse-Linked-List]]
|
||||
|
||||
|
||||
|
@ -45,9 +48,16 @@ tag:#leetcode
|
|||
|
||||
- [[Leetcode Valid-Parentheses]]
|
||||
- [[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 Implement-Queue-Using-Stacks]]
|
||||
- [[Leetcode Intersection-of-Two-Arrays-II]]
|
||||
- [[Leetcode Linked-List-Cycle]]
|
||||
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
|
||||
- [[Leetcode Maximum-Difference-Between-Increasing-Elements]]
|
||||
- [[Leetcode Maxinum-subarray]]
|
||||
- [[Leetcode Merge-Sorted-Array]]
|
||||
- [[Leetcode Merge-Two-Sorted-Lists]]
|
||||
|
@ -86,9 +96,15 @@ tag:#DS tag:#coding_problem -tag:#template_remove_me
|
|||
|
||||
- [[Leetcode Valid-Parentheses]]
|
||||
- [[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 Implement-Queue-Using-Stacks]]
|
||||
- [[Leetcode Intersection-of-Two-Arrays-II]]
|
||||
- [[Leetcode Linked-List-Cycle]]
|
||||
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
|
||||
- [[Leetcode Maxinum-subarray]]
|
||||
- [[Leetcode Merge-Sorted-Array]]
|
||||
- [[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 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 Intersection-of-Two-Arrays-II]]
|
||||
- [[Leetcode Linked-List-Cycle]]
|
||||
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
|
||||
- [[Leetcode Maximum-Difference-Between-Increasing-Elements]]
|
||||
- [[Leetcode Maxinum-subarray]]
|
||||
- [[Leetcode Merge-Sorted-Array]]
|
||||
- [[Leetcode Merge-Two-Sorted-Lists]]
|
||||
|
|
|
@ -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)
|
||||
|
||||
**Input:** root = [3,9,20,null,null,15,7]
|
||||
**Output:** [[3],[9,20],[15,7]]
|
||||
**Input:** root = `[3,9,20,null,null,15,7]`
|
||||
**Output:** `[[3],[9,20],[15,7]]`
|
||||
|
||||
**Example 2:**
|
||||
|
||||
**Input:** root = [1]
|
||||
**Output:** [[1]]
|
||||
**Output:** `[[1]]`
|
||||
|
||||
**Example 3:**
|
||||
|
||||
|
|
|
@ -38,8 +38,10 @@ Implement the `MyQueue` class:
|
|||
|
||||
#### Examples
|
||||
**Input**
|
||||
```
|
||||
["MyQueue", "push", "push", "peek", "pop", "empty"]
|
||||
[[], [1], [2], [], [], []]
|
||||
```
|
||||
**Output**
|
||||
[null, null, null, 1, 1, false]
|
||||
|
||||
|
|
|
@ -21,17 +21,87 @@ tag:#BFS
|
|||
|
||||
|
||||
##### Links:
|
||||
- [Link to problem]()
|
||||
- [Link to problem](https://leetcode.com/problems/maximum-depth-of-binary-tree/)
|
||||
___
|
||||
### 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
|
||||
|
||||
**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
|
||||
|
||||
- The number of nodes in the tree is in the range `[0, 104]`.
|
||||
- `-100 <= Node.val <= 100`
|
||||
|
||||
### Thoughts
|
||||
|
||||
> [!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
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
```
|
Loading…
Reference in a new issue