897. Increasing Order Search Tree
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 79% Topics: Stack, Tree, Depth-First Search, Binary Search Tree, Binary Tree
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(h)
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def increasingBST(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
def increasingBSTHelper(root, tail):
if not root:
return tail
result = increasingBSTHelper(root.left, root)
root.left = None
root.right = increasingBSTHelper(root.right, tail)
return result
return increasingBSTHelper(root, None)
Solution from kamyu104/LeetCode-Solutions · MIT