Trees, Recursion, And Binary Search Trees
Asked of: Software Engineer
Last updated

What's being tested
These problems test recursive tree traversal, careful base-case design, and choosing the right aggregation for each node’s subtree. You must distinguish general binary-tree logic from BST ordering, then state correctness, complexity, and edge-case behavior clearly.
Patterns & templates
-
Depth-first recursion — return subtree height or best contribution; define
nullexplicitly, typicallyO(n)time andO(h)call-stack space. -
N-ary tree height — compute
1 + max(child heights); initialize the maximum correctly and clarify whether depth counts nodes or edges. -
Postorder dynamic programming — combine left result, node value, and right result after recursive calls; useful for height balance and maximum path sum.
-
BST LCA — compare both targets with the current value; move left when both are smaller, right when both are larger, otherwise current node is the split point.
-
Catalan recurrence — distinct BST shapes satisfy ; use bottom-up DP and 64-bit integers for
n ≤ 19. -
Maximum path sum — return the best downward gain while separately tracking a path that bends through the node; clamp negative child gains to zero.
-
Level-order encoding — map array indices consistently, validate missing children, and avoid assuming every position represents an existing node in sparse representations.
Common pitfalls
Pitfall: Confusing node depth with edge depth, or returning only root-to-leaf paths when the maximum path may start and end anywhere.
Pitfall: Claiming
O(log n)for BST operations without stating the tree is balanced; an unbalanced BST can degrade toO(n).
Pitfall: Forgetting
null, single-node, duplicate-key, negative-value, and “target not present” behavior when explaining correctness.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Practice questions
- Choose Alice's Most Profitable Root-to-Leaf Route against BobAmazon · Software Engineer · Other · medium
- Measure the Maximum Root-to-Leaf Depth of an N-ary TreeAmazon · Software Engineer · Other · easy
- Highest-total path through a binary tree encoded in level orderAmazon · Software Engineer · Other · medium
- Count the Distinct Binary Search Tree Shapes for Keys 1 to nAmazon · Software Engineer · Other · medium
- Measure Infection Time from One Valued Binary-Tree NodeAmazon · Software Engineer · Other · medium
- Solve Three Algorithm VariantsAmazon · Software Engineer · Technical Screen · medium
- Return the Vertical Traversal of a Binary TreeAmazon · Software Engineer · Onsite · hard
- Implement DFS and tree algorithmsAmazon · Software Engineer · Technical Screen · medium
- Find LCA in a BSTAmazon · Software Engineer · Technical Screen · medium
Related concepts
- Trees, Recursion, And BST TraversalCoding & Algorithms
- Trees And Hierarchical StructuresCoding & Algorithms
- BST Algorithms And Lowest Common AncestorCoding & Algorithms
- Tree Traversal And Hierarchical StructuresCoding & Algorithms
- Tree And Linked Structure AlgorithmsCoding & Algorithms
- Binary Tree AlgorithmsCoding & Algorithms