Skip to content

Chapter 3 of 7

Trees and Hierarchical Structures

Binary trees and Binary Search Trees (BSTs) form the foundation of hierarchical data structures. A BST maintains the invariant that left children are less than the parent and right children are greater, enabling O(log n) average-case search and insert. Tree traversals come in three primary flavors: inorder (Left-Root-Right) yields sorted order for BSTs, preorder (Root-Left-Right) is used for serialization, and postorder (Left-Right-Root) is ideal for deletion operations. Level-order traversal uses BFS to visit nodes level by level, with variants like zigzag traversal alternating direction.

Advanced tree operations include finding the Lowest Common Ancestor (LCA), validating BST property, computing tree diameter, and detecting balanced height. The LCA problem has an elegant recursive solution for general binary trees, while BSTs leverage their sorted property for an even simpler iterative approach. The Maximum Path Sum problem requires careful tracking because the optimal path may pass through or end at any node. Tree construction from traversals combines preorder and inorder arrays with hash maps for O(n) reconstruction.

Specialized tree structures extend these concepts in powerful ways. The Trie (Prefix Tree) stores strings character by character, enabling O(m) prefix operations used in autocomplete and spell check. The Segment Tree and Binary Indexed Tree (Fenwick Tree) support efficient range queries with O(log n) operations, with the segment tree's lazy propagation variant extending this to range updates. More exotic structures like AVL trees, red-black trees, and treaps maintain balance to guarantee O(log n) operations. Morris Traversal achieves O(1) space tree traversal by using threaded binary trees, temporarily linking rightmost nodes back to ancestors.

All chapters
  1. 1Searching and Sorting Foundations
  2. 2Array Techniques and Problem Patterns
  3. 3Trees and Hierarchical Structures
  4. 4Graph Algorithms
  5. 5Dynamic Programming
  6. 6Advanced Data Structures
  7. 7String Algorithms and Specialized Techniques

Drill it

Reading is not remembering. These come from the Algorithms Code deck:

Q

What is Binary Search and what is its time complexity?<br><br>Write the implementation in Python.

Binary Search finds a target in a sorted array by repeatedly halving the search space.Time: O(log n) &nbsp;|&nbsp; Space: O(1)def binary_search(arr, target):...

Q

What is Bubble Sort and what is its time complexity?<br><br>Write the implementation in Python.

Bubble Sort repeatedly swaps adjacent elements if they are in the wrong order.Time: O(n²) &nbsp;|&nbsp; Space: O(1)def bubble_sort(arr): n = len(arr) for...

Q

What is Merge Sort and what is its time complexity?<br><br>Write the implementation in Python.

Merge Sort is a divide-and-conquer algorithm that splits the array in half, recursively sorts each half, then merges them.Time: O(n log n) &nbsp;|&nbsp; Space:...

Q

What is Quick Sort and what is its time complexity?<br><br>Write the implementation in Python.

Quick Sort picks a pivot, partitions elements around it, then recursively sorts each partition.Time: O(n log n) avg, O(n²) worst &nbsp;|&nbsp; Space: O(log n)de...