|
| 1 | +""" |
| 2 | +Given the root of a binary tree, determine if it is a valid binary search tree (BST). |
| 3 | +
|
| 4 | +A valid binary search tree is defined as follows: |
| 5 | +- The left subtree of a node contains only nodes with keys less than the node's key. |
| 6 | +- The right subtree of a node contains only nodes with keys greater than the node's key. |
| 7 | +- Both the left and right subtrees must also be binary search trees. |
| 8 | +
|
| 9 | +In effect, a binary tree is a valid BST if its nodes are sorted in ascending order. |
| 10 | +leetcode: https://leetcode.com/problems/validate-binary-search-tree/ |
| 11 | +
|
| 12 | +If n is the number of nodes in the tree then: |
| 13 | +Runtime: O(n) |
| 14 | +Space: O(1) |
| 15 | +""" |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +from collections.abc import Iterator |
| 19 | +from dataclasses import dataclass |
| 20 | + |
| 21 | + |
| 22 | +@dataclass |
| 23 | +class Node: |
| 24 | + data: float |
| 25 | + left: Node | None = None |
| 26 | + right: Node | None = None |
| 27 | + |
| 28 | + def __iter__(self) -> Iterator[float]: |
| 29 | + """ |
| 30 | + >>> root = Node(data=2.1) |
| 31 | + >>> list(root) |
| 32 | + [2.1] |
| 33 | + >>> root.left=Node(data=2.0) |
| 34 | + >>> list(root) |
| 35 | + [2.0, 2.1] |
| 36 | + >>> root.right=Node(data=2.2) |
| 37 | + >>> list(root) |
| 38 | + [2.0, 2.1, 2.2] |
| 39 | + """ |
| 40 | + if self.left: |
| 41 | + yield from self.left |
| 42 | + yield self.data |
| 43 | + if self.right: |
| 44 | + yield from self.right |
| 45 | + |
| 46 | + @property |
| 47 | + def is_sorted(self) -> bool: |
| 48 | + """ |
| 49 | + >>> Node(data='abc').is_sorted |
| 50 | + True |
| 51 | + >>> Node(data=2, |
| 52 | + ... left=Node(data=1.999), |
| 53 | + ... right=Node(data=3)).is_sorted |
| 54 | + True |
| 55 | + >>> Node(data=0, |
| 56 | + ... left=Node(data=0), |
| 57 | + ... right=Node(data=0)).is_sorted |
| 58 | + True |
| 59 | + >>> Node(data=0, |
| 60 | + ... left=Node(data=-11), |
| 61 | + ... right=Node(data=3)).is_sorted |
| 62 | + True |
| 63 | + >>> Node(data=5, |
| 64 | + ... left=Node(data=1), |
| 65 | + ... right=Node(data=4, left=Node(data=3))).is_sorted |
| 66 | + False |
| 67 | + >>> Node(data='a', |
| 68 | + ... left=Node(data=1), |
| 69 | + ... right=Node(data=4, left=Node(data=3))).is_sorted |
| 70 | + Traceback (most recent call last): |
| 71 | + ... |
| 72 | + TypeError: '<' not supported between instances of 'str' and 'int' |
| 73 | + >>> Node(data=2, |
| 74 | + ... left=Node([]), |
| 75 | + ... right=Node(data=4, left=Node(data=3))).is_sorted |
| 76 | + Traceback (most recent call last): |
| 77 | + ... |
| 78 | + TypeError: '<' not supported between instances of 'int' and 'list' |
| 79 | + """ |
| 80 | + if self.left and (self.data < self.left.data or not self.left.is_sorted): |
| 81 | + return False |
| 82 | + if self.right and (self.data > self.right.data or not self.right.is_sorted): |
| 83 | + return False |
| 84 | + return True |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + import doctest |
| 89 | + |
| 90 | + doctest.testmod() |
| 91 | + tree = Node(data=2.1, left=Node(data=2.0), right=Node(data=2.2)) |
| 92 | + print(f"Tree {list(tree)} is sorted: {tree.is_sorted = }.") |
| 93 | + assert tree.right |
| 94 | + tree.right.data = 2.0 |
| 95 | + print(f"Tree {list(tree)} is sorted: {tree.is_sorted = }.") |
| 96 | + tree.right.data = 2.1 |
| 97 | + print(f"Tree {list(tree)} is sorted: {tree.is_sorted = }.") |
0 commit comments