Time Space O(n).
Tag Archives: BST
Closest Binary Search Tree Value – Python 3 (Week 18 – 03)
Time O(n). Space O(n).
Binary Search Tree Iterator – Python 3 (Week 18 – 02)
Time O(n+k). Space O(n).
Binary Search Tree Iterator – Python 3 (Week 18 – 01)
Solution 1 Time, Space O(n). Solution 2
Inorder Successor in BST – Python 3 (Week 12 – 15)
class Solution: “”” @param: root: The root of the BST. @param: p: You need find the successor node of p. @return: Successor of p. “”” def inorderSuccessor(self, root, p): # write your code here if root == None or p == None: return None if root.val <= p.val: return self.inorderSuccessor(root.right, p) else: leftAns = self.inorderSuccessor(root.left, …
Continue reading “Inorder Successor in BST – Python 3 (Week 12 – 15)”
Convert BST to Greater Tree – Python 3 (Week 4 – 04)
The third solution is tricky and slow. But its space complexity is O(n). Solution 1 Time complexity is O(n). Space complexity is O(n). Solution 2 Time complexity is O(n). Space complexity is O(n). Solution 3 Time complexity is O(n). Space complexity is O(1).