vault backup: 2022-07-08 11:29:47
This commit is contained in:
parent
2b8bae8a59
commit
85d09906e2
2
.obsidian/graph.json
vendored
2
.obsidian/graph.json
vendored
|
@ -32,6 +32,6 @@
|
|||
"repelStrength": 10,
|
||||
"linkStrength": 1,
|
||||
"linkDistance": 250,
|
||||
"scale": 0.8153718570546582,
|
||||
"scale": 0.8153718570546572,
|
||||
"close": true
|
||||
}
|
|
@ -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]]
|
||||
|
|
41
OJ notes/pages/Breadth First Search.md
Normal file
41
OJ notes/pages/Breadth First Search.md
Normal file
|
@ -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
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -26,6 +26,7 @@ tag:#DFS
|
|||
- [[Leetcode Path-Sum]]
|
||||
- [[Leetcode Search-In-a-Binary-Tree]]
|
||||
- [[Leetcode Symmetric-Tree]]
|
||||
- [[Leetcode Validate-Binary-Search-Tree]]
|
||||
|
||||
|
||||
##### Links:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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.
|
||||
|
|
117
OJ notes/pages/Leetcode Two-Sum-IV-Input-Is-a-BST.md
Normal file
117
OJ notes/pages/Leetcode Two-Sum-IV-Input-Is-a-BST.md
Normal file
|
@ -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<int> uset;
|
||||
queue<TreeNode*> 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;
|
||||
}
|
||||
};
|
||||
```
|
Loading…
Reference in a new issue