Skip to content

Binary Search Tree

Table of Contents

270. Closest Binary Search Tree Value

  • LeetCode | LeetCode CH (Easy)

  • Tags: binary search, tree, depth first search, binary search tree, binary tree

272. Closest Binary Search Tree Value II

  • LeetCode | LeetCode CH (Hard)

  • Tags: two pointers, stack, tree, depth first search, binary search tree, heap priority queue, binary tree

255. Verify Preorder Sequence in Binary Search Tree

  • LeetCode | LeetCode CH (Medium)

  • Tags: array, stack, tree, binary search tree, recursion, monotonic stack, binary tree

255. Verify Preorder Sequence in Binary Search Tree - Python Solution
from typing import List


# BST
def verifyPreorder(preorder: List[int]) -> bool:
    stack = []
    low = float("-inf")

    for value in preorder:
        if value < low:
            return False
        while stack and value > stack[-1]:
            low = stack.pop()
        stack.append(value)

    return True


if __name__ == "__main__":
    assert verifyPreorder([8, 5, 1, 7, 10, 12]) is True
    assert verifyPreorder([8, 5, 4, 3, 2, 1]) is True

1214. Two Sum BSTs

  • LeetCode | LeetCode CH (Medium)

  • Tags: two pointers, binary search, stack, tree, depth first search, binary search tree, binary tree

333. Largest BST Subtree

  • LeetCode | LeetCode CH (Medium)

  • Tags: dynamic programming, tree, depth first search, binary search tree, binary tree

Comments