Trees and Tries Python practice problems
Traversal, recursion on hierarchies, binary-search-tree rules, prefix lookup, and subtree reasoning.
Core concepts
Problem-solving patterns
- tree · BFS
- tree · depth
- BST · bounds
- binary tree · recursion · LeetCode pattern
- BST · inorder · company screen
What this topic builds
Learn the idea once, use it across the exercises, and explain why your approach works.
All 26 problems in this topic
Work from top to bottom to build the concept gradually.
- #273Traverse a binary tree by levels: Three-level treeBeginnerPremium
Print each depth of a small complete tree on its own line.
- #274Traverse a binary tree by levels: Single-node treeBeginnerPremium
A one-node tree produces exactly one level.
- #275Find binary-tree maximum depth: Unbalanced treeIntermediatePremium
Find the depth of a tree whose longest path contains three nodes.
- #276Find binary-tree maximum depth: Empty treeIntermediatePremium
An empty tree represented by null has depth zero.
- #277Validate a binary search tree: Valid BSTIntermediatePremium
Confirm that all nodes obey the global binary-search-tree ordering rule.
- #278Validate a binary search tree: Hidden violationIntermediatePremium
Detect a value that is locally plausible but violates an ancestor's bound.
- #279Find lowest common ancestor: Targets in different subtreesIntermediatePremium
Find the lowest node whose subtree contains both requested values.
- #280Find lowest common ancestor: One node is an ancestorAdvancedPremium
Return the higher target when it already contains the other target.
- #281Find kth smallest value in a BST: Third smallestAdvancedPremium
Return the third value in the BST's increasing order.
- #282Find kth smallest value in a BST: Smallest valueAdvancedPremium
When k is one, return the minimum node.
- #283Serialize and deserialize a binary tree: Sparse tree round tripAdvancedPremium
Serialize a sparse tree, deserialize it, and show the preserved tokens.
- #284Serialize and deserialize a binary tree: Trim redundant trailing nullsAdvancedPremium
Remove trailing null markers that do not add structural information.
- #285Build a tree from preorder and inorder: Balanced reconstructionAdvancedPremium
Reconstruct a tree and print its level-order values.
- #286Build a tree from preorder and inorder: Right-skewed reconstructionAdvancedPremium
Build a tree whose nodes all lie on the right.
- #287View a binary tree from the right: Sparse right viewInterview-LevelPremium
Return the node visible from the right at every tree level.
- #288View a binary tree from the right: Left-only treeInterview-LevelPremium
A left child is visible when no node exists farther right on its level.
- #289Calculate binary-tree diameter: Diameter crosses rootInterview-LevelPremium
Find the longest edge-count path between any two nodes.
- #290Calculate binary-tree diameter: Skewed treeInterview-LevelPremium
The diameter of a one-sided chain is its number of edges.
- #291List root-to-leaf target paths: Two valid pathsInterview-LevelPremium
List every root-to-leaf path whose values reach the target.
- #292List root-to-leaf target paths: No target pathInterview-LevelPremium
Report None when no leaf completes the target sum.
- #293Implement a prefix trie: Word and prefix checksInterview-LevelPremium
Insert words, then distinguish exact matches from valid prefixes.
- #294Implement a prefix trie: Missing prefixInterview-LevelPremium
Return False when no inserted word begins with the requested prefix.
- #295Find dictionary words in a board: Several board wordsInterview-LevelPremium
Find all dictionary words that can be formed by adjacent cells without reuse.
- #296Find dictionary words in a board: No word can be formedInterview-LevelPremium
Report None when every dictionary word is impossible on the board.
- #297Find maximum binary-tree path sum: Best path crosses a subtree rootInterview-LevelPremium
Find the greatest sum along any connected path in the tree.
- #298Find maximum binary-tree path sum: All negative valuesInterview-LevelPremium
Choose the least negative single node when every extension would reduce the sum.