From 85d09906e28a77f3c20f701ebc1ac704696bc96b Mon Sep 17 00:00:00 2001 From: juan Date: Fri, 8 Jul 2022 11:29:47 +0800 Subject: [PATCH] vault backup: 2022-07-08 11:29:47 --- .obsidian/graph.json | 2 +- OJ notes/OJ-index.md | 21 ++++ OJ notes/pages/Breadth First Search.md | 41 ++++++ .../Leetcode Binary-Tree-Inorder-Traversal.md | 2 + ...tcode Binary-Tree-Level-Order-Traversal.md | 2 + ...eetcode Binary-Tree-Postorder-Traversal.md | 2 + ...Leetcode Binary-Tree-Preorder-Traversal.md | 2 + ...etcode Insert-Into-a-Binary-Search-Tree.md | 1 + .../Leetcode Maximum-Depth-Of-Binary-Tree.md | 2 + OJ notes/pages/Leetcode Path-Sum.md | 2 + .../pages/Leetcode Search-In-a-Binary-Tree.md | 3 + OJ notes/pages/Leetcode Symmetric-Tree.md | 15 ++- .../Leetcode Two-Sum-IV-Input-Is-a-BST.md | 117 ++++++++++++++++++ 13 files changed, 208 insertions(+), 4 deletions(-) create mode 100644 OJ notes/pages/Breadth First Search.md create mode 100644 OJ notes/pages/Leetcode Two-Sum-IV-Input-Is-a-BST.md diff --git a/.obsidian/graph.json b/.obsidian/graph.json index 84cfcd6..a10df69 100644 --- a/.obsidian/graph.json +++ b/.obsidian/graph.json @@ -32,6 +32,6 @@ "repelStrength": 10, "linkStrength": 1, "linkDistance": 250, - "scale": 0.8153718570546582, + "scale": 0.8153718570546572, "close": true } \ No newline at end of file diff --git a/OJ notes/OJ-index.md b/OJ notes/OJ-index.md index 844f0ce..ae919e0 100644 --- a/OJ notes/OJ-index.md +++ b/OJ notes/OJ-index.md @@ -34,6 +34,7 @@ tag:#CS_list_need_practicing - [ ] [[Leetcode Binary-Tree-Postorder-Traversal]] - [ ] [[Leetcode Reverse-Linked-List]] +- [ ] [[Leetcode Validate-Binary-Search-Tree]] @@ -54,7 +55,9 @@ tag:#leetcode - [[Leetcode Binary-Tree-Preorder-Traversal]] - [[Leetcode First-Unique-Character-In-a-String]] - [[Leetcode Implement-Queue-Using-Stacks]] +- [[Leetcode Insert-Into-a-Binary-Search-Tree]] - [[Leetcode Intersection-of-Two-Arrays-II]] +- [[Leetcode Invert-Binary-Tree]] - [[Leetcode Linked-List-Cycle]] - [[Leetcode Maximum-Depth-Of-Binary-Tree]] - [[Leetcode Maximum-Difference-Between-Increasing-Elements]] @@ -62,16 +65,20 @@ tag:#leetcode - [[Leetcode Merge-Sorted-Array]] - [[Leetcode Merge-Two-Sorted-Lists]] - [[Leetcode Pascal's-Triangle]] +- [[Leetcode Path-Sum]] - [[Leetcode Ransom-Note]] - [[Leetcode Remove-Duplicates-From-Sorted-List]] - [[Leetcode Remove-Linked-List-Elements]] - [[Leetcode Reshape-The-Matrix]] - [[Leetcode Reverse-Linked-List]] - [[Leetcode Search-a-2D-Matrix]] +- [[Leetcode Search-In-a-Binary-Tree]] - [[Leetcode Symmetric-Tree]] - [[Leetcode Two-Sum]] +- [[Leetcode Two-Sum-IV-Input-Is-a-BST]] - [[Leetcode Valid-Anagram]] - [[Leetcode Valid-Sudoku]] +- [[Leetcode Validate-Binary-Search-Tree]] @@ -103,23 +110,29 @@ tag:#DS tag:#coding_problem -tag:#template_remove_me - [[Leetcode Binary-Tree-Preorder-Traversal]] - [[Leetcode First-Unique-Character-In-a-String]] - [[Leetcode Implement-Queue-Using-Stacks]] +- [[Leetcode Insert-Into-a-Binary-Search-Tree]] - [[Leetcode Intersection-of-Two-Arrays-II]] +- [[Leetcode Invert-Binary-Tree]] - [[Leetcode Linked-List-Cycle]] - [[Leetcode Maximum-Depth-Of-Binary-Tree]] - [[Leetcode Maxinum-subarray]] - [[Leetcode Merge-Sorted-Array]] - [[Leetcode Merge-Two-Sorted-Lists]] - [[Leetcode Pascal's-Triangle]] +- [[Leetcode Path-Sum]] - [[Leetcode Ransom-Note]] - [[Leetcode Remove-Duplicates-From-Sorted-List]] - [[Leetcode Remove-Linked-List-Elements]] - [[Leetcode Reshape-The-Matrix]] - [[Leetcode Reverse-Linked-List]] - [[Leetcode Search-a-2D-Matrix]] +- [[Leetcode Search-In-a-Binary-Tree]] - [[Leetcode Symmetric-Tree]] - [[Leetcode Two-Sum]] +- [[Leetcode Two-Sum-IV-Input-Is-a-BST]] - [[Leetcode Valid-Anagram]] - [[Leetcode Valid-Sudoku]] +- [[Leetcode Validate-Binary-Search-Tree]] @@ -129,6 +142,7 @@ tag:#DS tag:#CS_analysis -tag:#template_remove_me - [[$filename]] ``` +- [[Breadth First Search]] - [[cpp_Range_based_for_loop]] - [[cpp_std_multiset]] - [[cpp_std_unordered_map]] @@ -149,7 +163,9 @@ tag:#algorithm tag:#coding_problem -tag:#template_remove_me - [[Leetcode Binary-Tree-Postorder-Traversal]] - [[Leetcode Binary-Tree-Preorder-Traversal]] - [[Leetcode First-Unique-Character-In-a-String]] +- [[Leetcode Insert-Into-a-Binary-Search-Tree]] - [[Leetcode Intersection-of-Two-Arrays-II]] +- [[Leetcode Invert-Binary-Tree]] - [[Leetcode Linked-List-Cycle]] - [[Leetcode Maximum-Depth-Of-Binary-Tree]] - [[Leetcode Maximum-Difference-Between-Increasing-Elements]] @@ -157,12 +173,16 @@ tag:#algorithm tag:#coding_problem -tag:#template_remove_me - [[Leetcode Merge-Sorted-Array]] - [[Leetcode Merge-Two-Sorted-Lists]] - [[Leetcode Pascal's-Triangle]] +- [[Leetcode Path-Sum]] - [[Leetcode Ransom-Note]] - [[Leetcode Reverse-Linked-List]] - [[Leetcode Search-a-2D-Matrix]] +- [[Leetcode Search-In-a-Binary-Tree]] - [[Leetcode Symmetric-Tree]] - [[Leetcode Two-Sum]] +- [[Leetcode Two-Sum-IV-Input-Is-a-BST]] - [[Leetcode Valid-Anagram]] +- [[Leetcode Validate-Binary-Search-Tree]] @@ -176,6 +196,7 @@ tag:#algorithm tag:#CS_analysis -tag:#template_remove_me ``` - [[Binary Search Algorithm]] +- [[Breadth First Search]] - [[cpp_std_sort]] - [[Floyd's Cycle Finding Algorithm]] - [[Kadane's Algorithm]] diff --git a/OJ notes/pages/Breadth First Search.md b/OJ notes/pages/Breadth First Search.md new file mode 100644 index 0000000..5533353 --- /dev/null +++ b/OJ notes/pages/Breadth First Search.md @@ -0,0 +1,41 @@ +# Breadth First Search + +#### 2022-07-08 11:19 + +> ##### Algorithms: +> #algorithm #BFS +> ##### Data structures: +> #DS #binary_tree +> ##### Difficulty: +> #CS_analysis #difficulty- +> ##### Additional tags: +> + +##### Related problems: +```expander +tag:#coding_problem tag:#BFS -tag:#template_remove_me +``` + +- [[Leetcode Binary-Tree-Level-Order-Traversal]] +- [[Leetcode Maximum-Depth-Of-Binary-Tree]] +- [[Leetcode Search-In-a-Binary-Tree]] +- [[Leetcode Symmetric-Tree]] +- [[Leetcode Two-Sum-IV-Input-Is-a-BST]] + + +##### Links: +- [cppreference]() +___ + +### What is Breadth First Search? +means BFS + +### How does Breadth First Search work? + +#### Example code + +### Why and when to use it? + +> [!tip] Whether to push root or root's nodes initially +> When checking the symmetry, [[Leetcode Symmetric-Tree]], push nodes to make sure the stack can be poped correctly. +> otherwise, check if the root node is needed and minimum amount of nodes in the tree is larger than 1 \ No newline at end of file diff --git a/OJ notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md b/OJ notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md index f46f49b..9b89ddc 100644 --- a/OJ notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md +++ b/OJ notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md @@ -20,11 +20,13 @@ tag:#DFS - [[Leetcode Binary-Tree-Postorder-Traversal]] - [[Leetcode Binary-Tree-Preorder-Traversal]] +- [[Leetcode Insert-Into-a-Binary-Search-Tree]] - [[Leetcode Invert-Binary-Tree]] - [[Leetcode Maximum-Depth-Of-Binary-Tree]] - [[Leetcode Path-Sum]] - [[Leetcode Search-In-a-Binary-Tree]] - [[Leetcode Symmetric-Tree]] +- [[Leetcode Validate-Binary-Search-Tree]] ##### Links: diff --git a/OJ notes/pages/Leetcode Binary-Tree-Level-Order-Traversal.md b/OJ notes/pages/Leetcode Binary-Tree-Level-Order-Traversal.md index 8806426..42507bb 100644 --- a/OJ notes/pages/Leetcode Binary-Tree-Level-Order-Traversal.md +++ b/OJ notes/pages/Leetcode Binary-Tree-Level-Order-Traversal.md @@ -20,6 +20,8 @@ tag:#BFS - [[Leetcode Maximum-Depth-Of-Binary-Tree]] - [[Leetcode Search-In-a-Binary-Tree]] +- [[Leetcode Symmetric-Tree]] +- [[Leetcode Two-Sum-IV-Input-Is-a-BST]] ##### Links: diff --git a/OJ notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md b/OJ notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md index 437dc90..d42bcd0 100644 --- a/OJ notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md +++ b/OJ notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md @@ -20,11 +20,13 @@ tag:#DFS - [[Leetcode Binary-Tree-Inorder-Traversal]] - [[Leetcode Binary-Tree-Preorder-Traversal]] +- [[Leetcode Insert-Into-a-Binary-Search-Tree]] - [[Leetcode Invert-Binary-Tree]] - [[Leetcode Maximum-Depth-Of-Binary-Tree]] - [[Leetcode Path-Sum]] - [[Leetcode Search-In-a-Binary-Tree]] - [[Leetcode Symmetric-Tree]] +- [[Leetcode Validate-Binary-Search-Tree]] ##### Links: diff --git a/OJ notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md b/OJ notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md index dcf2ccf..b8ae878 100644 --- a/OJ notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md +++ b/OJ notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md @@ -20,11 +20,13 @@ tag:#DFS - [[Leetcode Binary-Tree-Inorder-Traversal]] - [[Leetcode Binary-Tree-Postorder-Traversal]] +- [[Leetcode Insert-Into-a-Binary-Search-Tree]] - [[Leetcode Invert-Binary-Tree]] - [[Leetcode Maximum-Depth-Of-Binary-Tree]] - [[Leetcode Path-Sum]] - [[Leetcode Search-In-a-Binary-Tree]] - [[Leetcode Symmetric-Tree]] +- [[Leetcode Validate-Binary-Search-Tree]] ##### Links: diff --git a/OJ notes/pages/Leetcode Insert-Into-a-Binary-Search-Tree.md b/OJ notes/pages/Leetcode Insert-Into-a-Binary-Search-Tree.md index 0ed3f8b..52b0cc3 100644 --- a/OJ notes/pages/Leetcode Insert-Into-a-Binary-Search-Tree.md +++ b/OJ notes/pages/Leetcode Insert-Into-a-Binary-Search-Tree.md @@ -26,6 +26,7 @@ tag:#DFS - [[Leetcode Path-Sum]] - [[Leetcode Search-In-a-Binary-Tree]] - [[Leetcode Symmetric-Tree]] +- [[Leetcode Validate-Binary-Search-Tree]] ##### Links: diff --git a/OJ notes/pages/Leetcode Maximum-Depth-Of-Binary-Tree.md b/OJ notes/pages/Leetcode Maximum-Depth-Of-Binary-Tree.md index 320bb88..d5ebc7e 100644 --- a/OJ notes/pages/Leetcode Maximum-Depth-Of-Binary-Tree.md +++ b/OJ notes/pages/Leetcode Maximum-Depth-Of-Binary-Tree.md @@ -20,6 +20,8 @@ tag:#BFS - [[Leetcode Binary-Tree-Level-Order-Traversal]] - [[Leetcode Search-In-a-Binary-Tree]] +- [[Leetcode Symmetric-Tree]] +- [[Leetcode Two-Sum-IV-Input-Is-a-BST]] ##### Links: diff --git a/OJ notes/pages/Leetcode Path-Sum.md b/OJ notes/pages/Leetcode Path-Sum.md index 6640da7..bf54d78 100644 --- a/OJ notes/pages/Leetcode Path-Sum.md +++ b/OJ notes/pages/Leetcode Path-Sum.md @@ -21,10 +21,12 @@ tag:#DFS - [[Leetcode Binary-Tree-Inorder-Traversal]] - [[Leetcode Binary-Tree-Postorder-Traversal]] - [[Leetcode Binary-Tree-Preorder-Traversal]] +- [[Leetcode Insert-Into-a-Binary-Search-Tree]] - [[Leetcode Invert-Binary-Tree]] - [[Leetcode Maximum-Depth-Of-Binary-Tree]] - [[Leetcode Search-In-a-Binary-Tree]] - [[Leetcode Symmetric-Tree]] +- [[Leetcode Validate-Binary-Search-Tree]] ##### Links: diff --git a/OJ notes/pages/Leetcode Search-In-a-Binary-Tree.md b/OJ notes/pages/Leetcode Search-In-a-Binary-Tree.md index dee1e15..db2ef59 100644 --- a/OJ notes/pages/Leetcode Search-In-a-Binary-Tree.md +++ b/OJ notes/pages/Leetcode Search-In-a-Binary-Tree.md @@ -22,10 +22,13 @@ tag:#DFS OR tag:#BFS - [[Leetcode Binary-Tree-Level-Order-Traversal]] - [[Leetcode Binary-Tree-Postorder-Traversal]] - [[Leetcode Binary-Tree-Preorder-Traversal]] +- [[Leetcode Insert-Into-a-Binary-Search-Tree]] - [[Leetcode Invert-Binary-Tree]] - [[Leetcode Maximum-Depth-Of-Binary-Tree]] - [[Leetcode Path-Sum]] - [[Leetcode Symmetric-Tree]] +- [[Leetcode Two-Sum-IV-Input-Is-a-BST]] +- [[Leetcode Validate-Binary-Search-Tree]] ##### Links: diff --git a/OJ notes/pages/Leetcode Symmetric-Tree.md b/OJ notes/pages/Leetcode Symmetric-Tree.md index ca2b17c..c885667 100644 --- a/OJ notes/pages/Leetcode Symmetric-Tree.md +++ b/OJ notes/pages/Leetcode Symmetric-Tree.md @@ -3,7 +3,7 @@ #### 2022-07-05 10:15 > ##### Algorithms: -> #algorithm #DFS #recursion +> #algorithm #DFS #recursion #BFS > ##### Data structures: > #DS #binary_tree > ##### Difficulty: @@ -15,11 +15,20 @@ ##### Related topics: ```expander -tag:#DFS or tag:#BFS +tag:#DFS OR tag:#BFS ``` +- [[Leetcode Binary-Tree-Inorder-Traversal]] +- [[Leetcode Binary-Tree-Level-Order-Traversal]] +- [[Leetcode Binary-Tree-Postorder-Traversal]] +- [[Leetcode Binary-Tree-Preorder-Traversal]] +- [[Leetcode Insert-Into-a-Binary-Search-Tree]] +- [[Leetcode Invert-Binary-Tree]] - [[Leetcode Maximum-Depth-Of-Binary-Tree]] +- [[Leetcode Path-Sum]] - [[Leetcode Search-In-a-Binary-Tree]] +- [[Leetcode Two-Sum-IV-Input-Is-a-BST]] +- [[Leetcode Validate-Binary-Search-Tree]] ##### Links: @@ -111,7 +120,7 @@ public: }; ``` -Iteration, 8ms +BFS, iteration, 8ms ```cpp /** * Definition for a binary tree node. diff --git a/OJ notes/pages/Leetcode Two-Sum-IV-Input-Is-a-BST.md b/OJ notes/pages/Leetcode Two-Sum-IV-Input-Is-a-BST.md new file mode 100644 index 0000000..afdde98 --- /dev/null +++ b/OJ notes/pages/Leetcode Two-Sum-IV-Input-Is-a-BST.md @@ -0,0 +1,117 @@ +# Leetcode Two-Sum-IV-Input-Is-a-BST + +#### 2022-07-08 11:11 + +> ##### Algorithms: +> #algorithm #binary_search #BFS +> ##### Data structures: +> #DS #binary_tree #binary_search_tree +> ##### Difficulty: +> #coding_problem #difficulty-easy +> ##### Additional tags: +> #leetcode +> ##### Revisions: +> N/A + +##### Related topics: +```expander +tag:#BFS OR tag:#binary_search +``` + +- [[Binary Search Algorithm]] +- [[Leetcode Binary-Tree-Level-Order-Traversal]] +- [[Leetcode Maximum-Depth-Of-Binary-Tree]] +- [[Leetcode Search-a-2D-Matrix]] +- [[Leetcode Search-In-a-Binary-Tree]] +- [[Leetcode Symmetric-Tree]] + + +##### Links: +- [Link to problem](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) +- [Three method to solve this](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/discuss/106059/JavaC%2B%2B-Three-simple-methods-choose-one-you-like) +___ +### Problem + +Given the `root` of a Binary Search Tree and a target number `k`, return _`true` if there exist two elements in the BST such that their sum is equal to the given target_. + +#### Examples + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/09/21/sum_tree_1.jpg) + +**Input:** root = [5,3,6,2,4,null,7], k = 9 +**Output:** true + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2020/09/21/sum_tree_2.jpg) + +**Input:** root = [5,3,6,2,4,null,7], k = 28 +**Output:** false + +#### Constraints + +- The number of nodes in the tree is in the range `[1, 104]`. +- `-104 <= Node.val <= 104` +- `root` is guaranteed to be a **valid** binary search tree. +- `-105 <= k <= 105` + +### Thoughts + +> [!summary] +> This is a #BFS #hash_table problem. + +Mainly two methods: +1. #BFS with hash table. Time space O(n) + This can be quicker since you are starting at the middle, which is more likely to hit the answer, theoretically taking less time. +2. #binary_search. Time O(hn), h is the height of BST, best case h == log(n), worst case h == n + for every node, binary search in the tree for the answer. + +### Solution + +BFS with hash table +```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: + bool findTarget(TreeNode* root, int k) { + // BFS with unordered_set + // Take note: when to push root, when to push root->left and root->right + unordered_set uset; + queue pending; + pending.push(root); + + TreeNode* ptr; + while(!pending.empty()) { + ptr = pending.front(); + pending.pop(); + + // find first, to avoid k = 10, val = 5 + if (uset.find(ptr->val) != uset.end()) { + return true; + } + uset.insert(k - ptr->val); + + if (ptr->left) { + pending.push(ptr->left); + } + if (ptr->right) { + pending.push(ptr->right); + } + } + + return false; + } +}; +``` \ No newline at end of file