Binary Tree Algorithms
Asked of: Software Engineer
Last updated

What's being tested
Binary tree traversal under changing constraints: visibility, deletion/replacement, width indexing, reconstruction, and distance relationships. Interviewers are probing whether you can choose DFS, BFS, recursion, hashing, or parent pointers deliberately, while handling null roots, skewed trees, duplicate values, and overflow.
Patterns & templates
-
Right-side view BFS — level-order traversal, append last node per level;
O(n)time,O(w)space wherewis max width. -
Right-side view DFS — visit
root-right-left, record first node at each depth; cleaner recursion, but watch stack depth on skewed trees. -
Tree deletion / replacement — for BST-style deletion, handle 0/1/2 children; use inorder successor/predecessor; return updated subtree root.
-
Maximum width indexing — BFS with complete-tree positions: left
2*i, right2*i+1; normalize each level to avoid integer overflow. -
Build tree from traversals —
preorder + inorderorinorder + postorder; hashmap value-to-index givesO(n)time, recursive bounds prevent slicing overhead. -
Distance-k pairs / nodes — convert tree to undirected graph via parent map, then BFS; or use postorder distances for pair counting.
-
Validation mindset — confirm unique node values for reconstruction; malformed traversal arrays should fail fast or be documented as unsupported.
Common pitfalls
Pitfall: Treating width as node count per level; width includes missing positions between the leftmost and rightmost non-null nodes.
Pitfall: Reconstructing from traversal arrays with duplicates without extra identity information; the tree is not uniquely determined.
Pitfall: Forgetting to return the new root after deletion, especially when deleting the original root node.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Featured in interview prep guides
Practice questions
- Solve string grouping and tree right-view problemsTikTok · Software Engineer · Take-home Project · medium
- Delete nodes in linked list and binary treeTikTok · Software Engineer · Technical Screen · medium
- Compute maximum path sum in binary treeTikTok · Software Engineer · Technical Screen · medium
- Compute maximum path sum in a binary treeTikTok · Software Engineer · Technical Screen · hard
- Compute waits and find distance-k node pairsTikTok · Software Engineer · Technical Screen · Medium
- Compute rooms and verify tree completenessTikTok · Software Engineer · Technical Screen · Medium
- Implement binary tree in-order traversalTikTok · Software Engineer · Technical Screen · Medium
- Solve array and tree algorithm challengesTikTok · Software Engineer · Technical Screen · Medium
- Compute widest level span in a binary treeTikTok · Software Engineer · Take-home Project · Medium
- Reconstruct a tree from two traversalsTikTok · Software Engineer · Technical Screen · Medium
Related concepts
- Binary Tree Traversals, Vertical Order, And ViewsCoding & Algorithms
- Trees, Recursion, And BST TraversalCoding & Algorithms
- Tree And Linked Structure AlgorithmsCoding & Algorithms
- Trees And Hierarchical StructuresCoding & Algorithms
- Tree Traversal And Hierarchical StructuresCoding & Algorithms
- Trees, Tries, and Hierarchical DataCoding & Algorithms