Intermediate·Concept·2 min read

Trees and BST in Python

Practice this question

Tree node

Python
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

Problems to practice

  • Inorder traversal
  • Preorder traversal
  • Postorder traversal
  • Level order traversal
  • Maximum depth
  • Diameter of tree
  • Lowest common ancestor
  • Validate BST
  • Path sum

Example: inorder traversal

Python
def inorder(root):
    if not root:
        return []
    return inorder(root.left) + [root.val] + inorder(root.right)

Adapted from 18_DSA_PYTHON.md · “7. Trees and BST in Python”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.