அல்கோரிதம்inorder bst

BST inorder — sorted

TT
Testlaa Team
May 14, 20261 min read

Valid BST — inorder sorted. “Implicit sorted array”.

Try this in Python

from __future__ import annotations
from dataclasses import dataclass


@dataclass
class TreeNode:
    val: int
    left: TreeNode | None = None
    right: TreeNode | None = None


def inorder(root: TreeNode | None) -> list[int]:
    if not root:
        return []
    return inorder(root.left) + [root.val] + inorder(root.right)

# BST example
r = TreeNode(2, TreeNode(1), TreeNode(3))
print(inorder(r))

Key takeaways

  • Iterator — inorder stack.
  • k-th smallest.
  • Validate BST min/max.

Tags:

Tree structuresPythonமாணவர்கள்