Skip to content

Commit f4af35b

Browse files
committed
897 finish
1 parent 7740c60 commit f4af35b

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

Algorithms/0897.increasing-order-search-tree/increasing-order-search-tree.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,25 @@ func increasingBST(root *TreeNode) *TreeNode {
1616
}
1717

1818
func 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
}

0 commit comments

Comments
 (0)