Join Nostr
2026-10-28 09:07:48 UTC
in reply to

codementor on Nostr: The rule is about whole subtrees, not just children. A grandchild can be on the wrong ...

The rule is about whole subtrees, not just children. A grandchild can be on the wrong side of its grandparent.

```csharp
var bad = new Node(5, new Node(3, null, new Node(6)), new Node(8));
Console.WriteLine(LocalCheck(bad));

bool LocalCheck(Node? n) =>
n == null ||
(n.Left == null || n.Left.Value < n.Value) && (n.Right == null || n.Right.Value > n.Value)
&& LocalCheck(n.Left) && LocalCheck(n.Right);
```

It prints True, but the tree is not a BST: 6 sits in the left subtree of 5. Searching for 6 from the root would go right and miss it.

This is the most common bug in this question.

#tg005 · TG-005-A2