Trees and Binary Search Trees
📖 Introduction
🌳 Tree Fundamentals
Terminology
class TreeNode:
"""Basic tree node."""
def __init__(self, val):
self.val = val
self.children = []
# Terminology:
# - Root: Node with no parent (top of tree)
# - Leaf: Node with no children (bottom of tree)
# - Parent: Node directly above in hierarchy
# - Child: Node directly below in hierarchy
# - Siblings: Nodes sharing same parent
# - Depth: Distance from root (root depth = 0)
# - Height: Distance to furthest leaf
# - Subtree: A node and all its descendantsBinary Tree
Types of Binary Trees
🔄 Tree Traversals
Depth-First Search (DFS)
When to Use Each Traversal
Traversal
Use Case
Breadth-First Search (BFS)
🔍 Binary Search Tree (BST)
BST Operations
BST Properties
📊 Common Tree Problems
Maximum Depth
Same Tree / Symmetric Tree
Invert Binary Tree
Path Sum
Lowest Common Ancestor
Diameter of Binary Tree
Balanced Binary Tree
Serialize and Deserialize
Build Tree from Traversals
🌲 Special Tree Types
N-ary Tree
Trie (Prefix Tree)
📝 Practice Exercises
Exercise 1: Right Side View
Exercise 2: Flatten Binary Tree to Linked List
Exercise 3: Maximum Path Sum
🔑 Key Takeaways
🚀 What's Next?
📚 References
Last updated