Skip to content

Commit 16a3607

Browse files
committed
update
1 parent 7319869 commit 16a3607

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

897.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
TreeNode* increasingBST(TreeNode* root) {
13+
if(root == NULL){
14+
return root;
15+
}
16+
17+
TreeNode* left = root->left;
18+
TreeNode* right = root->right;
19+
20+
root->left = NULL;
21+
root->right = NULL;
22+
23+
TreeNode* newRoot = increasingBST(left);
24+
25+
TreeNode* newRight = increasingBST(right);
26+
27+
root->right = newRight;
28+
if(newRoot == NULL){
29+
return root;
30+
}
31+
32+
TreeNode* lastRight = newRoot;
33+
while(lastRight && lastRight->right){
34+
lastRight = lastRight->right;
35+
}
36+
lastRight -> right = root;
37+
return newRoot;
38+
}
39+
};

0 commit comments

Comments
 (0)