From bd01b6aa6a8eaa388905272d61cfde0380e4d1d1 Mon Sep 17 00:00:00 2001 From: eat-swap Date: Sun, 17 Apr 2022 12:07:13 +0800 Subject: [PATCH] add: 220414 [cpp] --- cpp/2204/220414.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 cpp/2204/220414.cpp diff --git a/cpp/2204/220414.cpp b/cpp/2204/220414.cpp new file mode 100644 index 0000000..e975735 --- /dev/null +++ b/cpp/2204/220414.cpp @@ -0,0 +1,28 @@ +#include + +struct TreeNode { + int val; + TreeNode* left; + TreeNode* right; + + explicit TreeNode(int x = 0, TreeNode* l = nullptr, TreeNode* r = nullptr) : val(x), left(l), right(r) {} +}; + +/** + * 700. Search in a Binary Search Tree + * 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. + */ + +class Solution { +public: + TreeNode* searchBST(TreeNode* root, int val) { + if (!root || val == root->val) + return root; + return searchBST(val < root->val ? root->left : root->right, val); + } +}; + +int main() { + return 0; +} \ No newline at end of file