vault backup: 2022-07-07 08:16:24

This commit is contained in:
juan 2022-07-07 08:16:24 +08:00
parent 527ee4ea89
commit ceb36abb66
10 changed files with 191 additions and 0 deletions

View file

@ -0,0 +1,24 @@
{
"commitMessage": "vault backup: {{date}}",
"autoCommitMessage": "vault backup: {{date}}",
"commitDateFormat": "YYYY-MM-DD HH:mm:ss",
"autoSaveInterval": 5,
"autoPushInterval": 0,
"autoPullInterval": 0,
"autoPullOnBoot": false,
"disablePush": false,
"pullBeforePush": true,
"disablePopups": false,
"listChangedFilesInMessageBody": false,
"showStatusBar": true,
"updateSubmodules": false,
"syncMethod": "merge",
"gitPath": "",
"customMessageOnAutoBackup": false,
"autoBackupAfterFileChange": true,
"treeStructure": false,
"refreshSourceControl": true,
"basePath": "",
"differentIntervalCommitAndPush": true,
"changedFilesInStatusBar": false
}

View file

@ -20,7 +20,10 @@ tag:#DFS
- [[Leetcode Binary-Tree-Postorder-Traversal]]
- [[Leetcode Binary-Tree-Preorder-Traversal]]
- [[Leetcode Invert-Binary-Tree]]
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
- [[Leetcode Path-Sum]]
- [[Leetcode Search-In-a-Binary-Tree]]
- [[Leetcode Symmetric-Tree]]

View file

@ -19,6 +19,7 @@ tag:#BFS
```
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
- [[Leetcode Search-In-a-Binary-Tree]]
##### Links:

View file

@ -20,7 +20,10 @@ tag:#DFS
- [[Leetcode Binary-Tree-Inorder-Traversal]]
- [[Leetcode Binary-Tree-Preorder-Traversal]]
- [[Leetcode Invert-Binary-Tree]]
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
- [[Leetcode Path-Sum]]
- [[Leetcode Search-In-a-Binary-Tree]]
- [[Leetcode Symmetric-Tree]]

View file

@ -20,7 +20,10 @@ tag:#DFS
- [[Leetcode Binary-Tree-Inorder-Traversal]]
- [[Leetcode Binary-Tree-Postorder-Traversal]]
- [[Leetcode Invert-Binary-Tree]]
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
- [[Leetcode Path-Sum]]
- [[Leetcode Search-In-a-Binary-Tree]]
- [[Leetcode Symmetric-Tree]]

View file

@ -18,6 +18,8 @@
tag:#recursion tag:#DFS
```
- [[Leetcode Path-Sum]]
- [[Leetcode Symmetric-Tree]]
##### Links:

View file

@ -19,6 +19,7 @@ tag:#BFS
```
- [[Leetcode Binary-Tree-Level-Order-Traversal]]
- [[Leetcode Search-In-a-Binary-Tree]]
##### Links:

View file

@ -18,6 +18,13 @@
tag:#DFS
```
- [[Leetcode Binary-Tree-Inorder-Traversal]]
- [[Leetcode Binary-Tree-Postorder-Traversal]]
- [[Leetcode Binary-Tree-Preorder-Traversal]]
- [[Leetcode Invert-Binary-Tree]]
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
- [[Leetcode Search-In-a-Binary-Tree]]
- [[Leetcode Symmetric-Tree]]
##### Links:

View file

@ -0,0 +1,146 @@
# Leetcode Search-In-a-Binary-Tree
#### 2022-07-07 08:06
> ##### Algorithms:
> #algorithm #DFS #BFS
> ##### Data structures:
> #DS #binary_tree
> ##### Difficulty:
> #coding_problem #difficulty-easy
> ##### Additional tags:
> #leetcode
> ##### Revisions:
> N/A
##### Related topics:
```expander
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 Invert-Binary-Tree]]
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
- [[Leetcode Path-Sum]]
- [[Leetcode Symmetric-Tree]]
##### Links:
- [Link to problem](https://leetcode.com/problems/search-in-a-binary-search-tree/submissions/)
___
### Problem
You are given the `root` of a binary search tree (BST) and an integer `val`.
Find the node in the BST that the node's value equals `val` and return the subtree rooted with that node. If such a node does not exist, return `null`.
#### Examples
Example 1:
Input: root = [4,2,7,1,3], val = 2
Output: [2,1,3]
Example 2:
Input: root = [4,2,7,1,3], val = 5
Output: []
#### Constraints
- The number of nodes in the tree is in the range `[1, 5000]`.
- `1 <= Node.val <= 107`
- `root` is a binary search tree.
- `1 <= val <= 107`
### Thoughts
> [!summary]
> This is a #DFS or #BFS problem. We search values in the tree.
### Solution
DFS
```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:
TreeNode *searchBST(TreeNode *root, int val) {
// DFS preorder
// Base cases
if (!root) {
return nullptr;
}
if (root->val == val) {
return root;
}
auto left = searchBST(root->left, val);
if (left) {
return left;
}
auto right = searchBST(root->right, val);
if (right) {
return right;
}
// left and right not found
return nullptr;
}
};
```
Which can be simplified to
```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:
TreeNode* searchBST(TreeNode* root, int val) {
// DFS preorder
// Base cases
if (!root) {
return nullptr;
}
if (root->val == val) {
return root;
}
auto left = searchBST(root->left, val);
if (left) {
return left;
}
return searchBST(root->right, val);
}
};
```
BFS
```cpp
```

View file

@ -19,6 +19,7 @@ tag:#DFS or tag:#BFS
```
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
- [[Leetcode Search-In-a-Binary-Tree]]
##### Links: