Skip to main content

Binary Tree Problems

Common algorithmic problems involving binary trees and their solutions.

Path sum problems

  • Path Sum: Check if tree has root-to-leaf path with given sum
  • Path Sum II: Find all root-to-leaf paths with given sum
  • Path Sum III: Count paths with given sum (path can start/end anywhere)
  • Maximum Path Sum: Find maximum sum of any path in tree

Tree diameter & width

  • Tree Diameter: Length of longest path between any two nodes
  • Tree Width: Maximum number of nodes at any level
  • Binary Tree Width: Width of binary tree at each level
  • Maximum Width: Find maximum width of binary tree

Lowest common ancestor

  • LCA in Binary Tree: Find lowest common ancestor of two nodes
  • LCA in BST: Optimized LCA for binary search trees
  • LCA with Parent Pointers: Using parent pointers for LCA
  • Distance between Nodes: Using LCA to find distance

Tree views (left, right, top, bottom)

  • Left View: Nodes visible from left side of tree
  • Right View: Nodes visible from right side of tree
  • Top View: Nodes visible from top of tree
  • Bottom View: Nodes visible from bottom of tree

Tree transformation

  • Invert Binary Tree: Swap left and right children of all nodes
  • Flatten Binary Tree: Convert tree to linked list
  • Tree to Array: Convert tree to array representation
  • Mirror Tree: Create mirror image of binary tree