add: 220414 [cpp]

This commit is contained in:
eat-swap 2022-04-17 12:07:13 +08:00
parent 288ea8a3e0
commit bd01b6aa6a
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
1 changed files with 28 additions and 0 deletions

28
cpp/2204/220414.cpp Normal file
View File

@ -0,0 +1,28 @@
#include <iostream>
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;
}