add: 220417 [cpp]

This commit is contained in:
eat-swap 2022-04-17 12:43:12 +08:00
parent bd01b6aa6a
commit 81ee098e42
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 36 additions and 1 deletions

35
cpp/2204/220417.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <functional>
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) {}
};
class Solution {
public:
static TreeNode* increasingBST(TreeNode* root, TreeNode* greater = nullptr) {
if (!root)
return nullptr;
root->right = root->right ? increasingBST(root->right, greater) : greater;
if (!root->left)
return root;
auto* rl = root->left;
root->left = nullptr;
return increasingBST(rl, root);
}
};
int main() {
auto* root = new TreeNode(
5,
new TreeNode(3, new TreeNode(2, new TreeNode(1)), new TreeNode(4)),
new TreeNode(6, nullptr, new TreeNode(8, new TreeNode(7), new TreeNode(9)))
);
auto* newRoot = Solution::increasingBST(root);
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2204)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2204 220416-CN.cpp) ADD_EXECUTABLE(2204 220417.cpp)