Skip to content

Commit 3a5351a

Browse files
committed
Runtime: 44 ms (Top 21.66%) | Memory: 17.30 MB (Top 23.65%)
1 parent 5391f5e commit 3a5351a

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
# Runtime: 22 ms (Top 13.1%) | Memory: 13.44 MB (Top 40.5%)
1+
// Runtime: 44 ms (Top 21.66%) | Memory: 17.30 MB (Top 23.65%)
22

3-
class Solution(object):
4-
5-
def increasingBST(self, root):
3+
class Solution:
4+
def increasingBST(self, node: TreeNode) -> TreeNode:
5+
dummy = tail = TreeNode()
6+
while node is not None:
7+
if node.left is not None:
8+
predecessor = node.left
9+
while predecessor.right is not None:
10+
predecessor = predecessor.right
11+
12+
predecessor.right = node
13+
left, node.left = node.left, None
14+
node = left
15+
else:
16+
tail.right = tail = node
17+
node = node.right
618

7-
def sortBST(node):
8-
if not node: return []
9-
10-
# return the in order BST nodes in list
11-
return sortBST(node.left) + [node.val] + sortBST(node.right)
12-
13-
# the in order sorted list of the tree nodes
14-
sorted_list = sortBST(root)
15-
16-
# generate new tree: temp for update, ans for return the root
17-
ans = temp = TreeNode(sorted_list[0])
18-
19-
# insert nodes to the right side of the new tree
20-
for i in range(1, len(sorted_list)):
21-
temp.right = TreeNode(sorted_list[i])
22-
temp = temp.right
23-
24-
return ans
19+
return dummy.right

0 commit comments

Comments
 (0)