-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path897.cpp
39 lines (33 loc) · 925 Bytes
/
897.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* increasingBST(TreeNode* root) {
if(root == NULL){
return root;
}
TreeNode* left = root->left;
TreeNode* right = root->right;
root->left = NULL;
root->right = NULL;
TreeNode* newRoot = increasingBST(left);
TreeNode* newRight = increasingBST(right);
root->right = newRight;
if(newRoot == NULL){
return root;
}
TreeNode* lastRight = newRoot;
while(lastRight && lastRight->right){
lastRight = lastRight->right;
}
lastRight -> right = root;
return newRoot;
}
};