File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments