diff --git a/.obsidian/plugins/obsidian-git/data.json b/.obsidian/plugins/obsidian-git/data.json index e69de29..3f2a630 100644 --- a/.obsidian/plugins/obsidian-git/data.json +++ b/.obsidian/plugins/obsidian-git/data.json @@ -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 +} \ No newline at end of file diff --git a/CS notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md b/CS notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md index 3b53a6d..f46f49b 100644 --- a/CS notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md +++ b/CS notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md @@ -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]] 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 bd11bf1..8806426 100644 --- a/CS notes/pages/Leetcode Binary-Tree-Level-Order-Traversal.md +++ b/CS notes/pages/Leetcode Binary-Tree-Level-Order-Traversal.md @@ -19,6 +19,7 @@ tag:#BFS ``` - [[Leetcode Maximum-Depth-Of-Binary-Tree]] +- [[Leetcode Search-In-a-Binary-Tree]] ##### Links: diff --git a/CS notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md b/CS notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md index ffd4ed2..437dc90 100644 --- a/CS notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md +++ b/CS notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md @@ -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]] diff --git a/CS notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md b/CS notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md index fd1bec3..dcf2ccf 100644 --- a/CS notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md +++ b/CS notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md @@ -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]] diff --git a/CS notes/pages/Leetcode Invert-Binary-Tree.md b/CS notes/pages/Leetcode Invert-Binary-Tree.md index 13fcca3..8e2cb31 100644 --- a/CS notes/pages/Leetcode Invert-Binary-Tree.md +++ b/CS notes/pages/Leetcode Invert-Binary-Tree.md @@ -18,6 +18,8 @@ tag:#recursion tag:#DFS ``` +- [[Leetcode Path-Sum]] +- [[Leetcode Symmetric-Tree]] ##### Links: 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 2246e2b..320bb88 100644 --- a/CS notes/pages/Leetcode Maximum-Depth-Of-Binary-Tree.md +++ b/CS notes/pages/Leetcode Maximum-Depth-Of-Binary-Tree.md @@ -19,6 +19,7 @@ tag:#BFS ``` - [[Leetcode Binary-Tree-Level-Order-Traversal]] +- [[Leetcode Search-In-a-Binary-Tree]] ##### Links: diff --git a/CS notes/pages/Leetcode Path-Sum.md b/CS notes/pages/Leetcode Path-Sum.md index ead4753..6640da7 100644 --- a/CS notes/pages/Leetcode Path-Sum.md +++ b/CS notes/pages/Leetcode Path-Sum.md @@ -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: diff --git a/CS notes/pages/Leetcode Search-In-a-Binary-Tree.md b/CS notes/pages/Leetcode Search-In-a-Binary-Tree.md new file mode 100644 index 0000000..0291592 --- /dev/null +++ b/CS notes/pages/Leetcode Search-In-a-Binary-Tree.md @@ -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 + +``` \ No newline at end of file diff --git a/CS notes/pages/Leetcode Symmetric-Tree.md b/CS notes/pages/Leetcode Symmetric-Tree.md index 76dc9c4..ca2b17c 100644 --- a/CS notes/pages/Leetcode Symmetric-Tree.md +++ b/CS notes/pages/Leetcode Symmetric-Tree.md @@ -19,6 +19,7 @@ tag:#DFS or tag:#BFS ``` - [[Leetcode Maximum-Depth-Of-Binary-Tree]] +- [[Leetcode Search-In-a-Binary-Tree]] ##### Links: