diff --git a/.obsidian/graph.json b/.obsidian/graph.json index 4144d8b..764f226 100644 --- a/.obsidian/graph.json +++ b/.obsidian/graph.json @@ -32,6 +32,6 @@ "repelStrength": 10, "linkStrength": 1, "linkDistance": 250, - "scale": 0.9752800867986091, + "scale": 0.975280086798608, "close": true } \ No newline at end of file diff --git a/CS notes/CS-index.md b/CS notes/CS-index.md index f77c26d..6da4a76 100644 --- a/CS notes/CS-index.md +++ b/CS notes/CS-index.md @@ -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]] diff --git a/CS notes/pages/Leetcode Binary-Tree-Level-Order-Traversal.md b/CS notes/pages/Leetcode Binary-Tree-Level-Order-Traversal.md index 50b5053..9091656 100644 --- a/CS notes/pages/Leetcode Binary-Tree-Level-Order-Traversal.md +++ b/CS notes/pages/Leetcode Binary-Tree-Level-Order-Traversal.md @@ -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:** @@ -58,7 +58,7 @@ In contrary to DFS, BFS uses queue. and there are many tricks for pushing 2d arr vector> vec; vec.push_back({}); vec.back().push_back(5); -// [[5]] +// [[ 5 ]] ``` ### Solution diff --git a/CS notes/pages/Leetcode Implement-Queue-Using-Stacks.md b/CS notes/pages/Leetcode Implement-Queue-Using-Stacks.md index 52a57ef..7f12b69 100644 --- a/CS notes/pages/Leetcode Implement-Queue-Using-Stacks.md +++ b/CS notes/pages/Leetcode Implement-Queue-Using-Stacks.md @@ -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] diff --git a/CS notes/pages/Leetcode Maximum-Depth-Of-Binary-Tree.md b/CS notes/pages/Leetcode Maximum-Depth-Of-Binary-Tree.md index 14bb4e4..b6b0c3b 100644 --- a/CS notes/pages/Leetcode Maximum-Depth-Of-Binary-Tree.md +++ b/CS notes/pages/Leetcode Maximum-Depth-Of-Binary-Tree.md @@ -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 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; + } +}; +``` \ No newline at end of file