Skip to content

Commit ae82d0b

Browse files
author
李钊
committed
🎉 pass: tree rearrange
1 parent a65df9e commit ae82d0b

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

897.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
3+
struct TreeNode
4+
{
5+
int val;
6+
TreeNode *left;
7+
TreeNode *right;
8+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
9+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
10+
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
11+
};
12+
13+
class Solution
14+
{
15+
public:
16+
TreeNode *increasingBST(TreeNode *root, TreeNode *tail = nullptr) {
17+
if (!root)
18+
return tail;
19+
root->right = increasingBST(root->right, tail);
20+
auto left = root->left;
21+
root->left = nullptr;
22+
return increasingBST(left, root);
23+
}
24+
};
25+
26+
int main(int argc, char const *argv[]) {
27+
Solution s;
28+
TreeNode *root = new TreeNode(5, new TreeNode(3, new TreeNode(2, new TreeNode(1), nullptr), new TreeNode(4)), new TreeNode(6, nullptr, new TreeNode(8, new TreeNode(7), new TreeNode(9))));
29+
TreeNode *ret = s.increasingBST(root);
30+
return 0;
31+
}

0 commit comments

Comments
 (0)