File tree Expand file tree Collapse file tree 1 file changed +10
-11
lines changed
Algorithms/0897.increasing-order-search-tree Expand file tree Collapse file tree 1 file changed +10
-11
lines changed Original file line number Diff line number Diff line change @@ -16,26 +16,25 @@ func increasingBST(root *TreeNode) *TreeNode {
1616}
1717
1818func helper (root * TreeNode ) (* TreeNode , * TreeNode ) {
19- if root == nil {
20- return nil , nil
21- }
22-
2319 if root .Left == nil && root .Right == nil {
2420 return root , root
2521 }
2622
27- if root .Left != nil {
28- leftRoot , leftRight := helper (root .Left )
23+ left , right := root .Left , root .Right
24+ root .Left , root .Right = nil , nil
25+
26+ if left != nil {
27+ leftRoot , leftRight := helper (left )
2928 leftRight .Right = root
30- if root . Right != nil {
31- rightRoot , rightRight := helper (root . Right )
29+ if right != nil {
30+ rightRoot , rightRight := helper (right )
3231 root .Right = rightRoot
3332 return leftRoot , rightRight
34- } else {
35- return leftRoot , root
3633 }
34+ return leftRoot , root
3735 }
38- rightRoot , rightRight := helper (root .Right )
36+
37+ rightRoot , rightRight := helper (right )
3938 root .Right = rightRoot
4039 return root , rightRight
4140}
You can’t perform that action at this time.
0 commit comments