diff --git a/2201/220112.cpp b/2201/220112.cpp new file mode 100644 index 0000000..3830448 --- /dev/null +++ b/2201/220112.cpp @@ -0,0 +1,38 @@ +#include + +/** + * Definition for a binary tree node. + */ +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + explicit TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + ~TreeNode() { delete this->left; delete this->right; } +}; + +class Solution { +public: + static TreeNode* insertIntoBST(TreeNode* root, int val) { + if (!root) + return new TreeNode(val); + if (val > root->val) { + if (root->right) { + insertIntoBST(root->right, val); + } else { + root->right = new TreeNode(val); + } + } else if (val < root->val) { + if (root->left) { + insertIntoBST(root->left, val); + } else { + root->left = new TreeNode(val); + } + } else { + return nullptr; + } + return root; + } +}; \ No newline at end of file diff --git a/2201/CMakeLists.txt b/2201/CMakeLists.txt index 20a78c9..9311585 100644 --- a/2201/CMakeLists.txt +++ b/2201/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2201) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2201 220112-CN.cpp) \ No newline at end of file +ADD_EXECUTABLE(2201 220112.cpp) \ No newline at end of file