All puzzles › Trees
Trees Practice Problems
35 puzzles (8 easy, 16 medium, 11 hard), 2 free, in Python, JavaScript and C++.
A tree is a structure where every node has children and exactly one path leads to each of them. Most tree problems have the same skeleton: solve the question for the left subtree, solve it for the right, and combine the two answers at the node you are standing on. Learn that shape and a whole family of problems opens up.
How to spot a trees problem
If the input is a root node, think recursively first and ask what a node needs to learn from its children. Level by level questions want a queue instead. In a binary search tree, remember that everything to the left is smaller and everything to the right is larger, which usually lets you skip half the tree.
New to it? Read the lesson first: Trees.
The puzzles, easiest first
- Biggest in the Tree (Easy): Given a binary tree, return its biggest value, or null if it's empty.
- Count Tree Nodes (Easy): Given a binary tree, return how many nodes it has.
- Full Nodes (Easy): Given a binary tree, return how many nodes have BOTH a left and a right child.
- Leftmost Value (Easy): Given a binary search tree, return its smallest value, or null if it's empty.
- Level Order (Easy): Given a binary tree, return every value level by level, top to bottom and left to right.
- Read the Root (Easy): Given a binary tree, return the root's value, or null if the tree is empty.
- Search a BST (Easy): Given a binary search tree and a target, return true if the target is in it.
- Sum of Tree Nodes (Easy): Given a binary tree, return the sum of every node's value.
- Max Depth of Binary Tree (Medium, free): Given a binary tree, return how many levels deep it goes.
- Count the Leaves (Medium): Given a binary tree, return how many leaves it has, where a leaf is a node with no children.
- How Deep Does It Go (Medium): Given a page table tree where a nonzero value is a real mapping and 0 is empty, return how many levels down the deepest mapping is.
- How Far Before It Dies (Medium): Given a distribution tree and a starting TTL, return how many nodes get the packet.
- Inorder Walk (Medium): Given a binary tree, return its values in inorder: the left subtree first, then the node, then the right subtree.
- Invert Binary Tree (Medium): Given a binary tree, mirror it so every left child swaps places with its right child, and return it.
- Mirror Image (Medium): Given a binary tree, return true if it's a mirror image of itself.
- Postorder Walk (Medium): Given a binary tree, return its values in postorder: the left subtree, then the right subtree, then the node.
- Preorder Walk (Medium): Given a binary tree, return its values in preorder: the node first, then its left subtree, then its right subtree.
- Prune the Branches Nobody Watches (Medium): Given a tree where a node holds 1 if it has a subscriber and 0 if it doesn't, return how many nodes are left after pruning.
- Same Tree? (Medium): Given two binary trees, return true only if they have the same shape and the same values in the same places.
- Shallowest Leaf (Medium): Given a binary tree, return the number of levels from the root down to the CLOSEST leaf.
- Sum Each Level (Medium): Given a binary tree, return the sum of each level, top to bottom.
- The Right-Side View (Medium): Given a binary tree, return what you'd see standing on its right side: the rightmost value on each level, top to bottom.
- The Tightest Budget Wins (Medium): Given a tree of resource groups where each node sets a limit, and a path down from the root (0 means left, 1 means right), return the limit that actually applies at the end of the path.
- Where Is the Sword, Really? (Medium): In a scene graph every object is placed relative to its parent: a sword relative to a hand, the hand relative to an arm, the arm relative to the knight.
- Stop Looking When It Cannot Matter (Hard, free): Minimax looks at every position in a game tree.
- Find the Corrupted Block (Hard): Given a tree where every parent should equal the sum of its two children, return how many parents don't.
- Is It a Search Tree? (Hard): Given a binary tree, return true if it's a valid binary search tree, where everything on the left is smaller and everything on the right is bigger.
- Is It Balanced? (Hard): Given a binary tree, return true if at every node, the left and right subtrees' heights differ by at most 1.
- Lowest Common Ancestor (Hard): Given a binary tree and two values p and q, return the value of their lowest common ancestor, the deepest node that has both below it (or is one of them).
- Path to a Target (Hard): Given a binary tree and a target, return true if some path from the root down to a LEAF adds up to exactly the target.
- The Deepest Match Wins (Hard): Given a routing tree and an address as a list of bits, return the next hop of the most specific route that matches, or 0 if none does.
- Translate the Address (Hard): Given a page table as a tree and an address as one index per level (0 means left, 1 means right), return the physical frame number, or -1 for a page fault.
- Two Routes That Could Be One (Hard): Given a routing tree where each node holds a next-hop ID, or 0 for no route, return how many nodes have two children that could be merged into it.
- What Does the Guard Do? (Hard): A guard's AI is a behavior tree.
- Widest Span (Hard): Given a binary tree, return the length, in edges, of the longest path between any two nodes.
Practice another pattern
Or browse by subject
Browse all 676 puzzles