Skip to content

Tips & Common Mistakes


MistakeWhy it’s badHow to avoid
❌ Forgetting the base case for nullCauses null reference errorsAlways start: if (node === null) return ...
❌ Wrong return type from recursionReturning void when you need a valueBe explicit: decide what each recursive call should return
❌ Modifying shared state incorrectlyMutating an array/path without backtrackingRemember to pop() after recursive call in path-tracking problems
❌ Recomputing height twiceCauses O(N²) instead of O(N)Return height from the recursive function itself; compute once

Edge CaseWhat to check
root === nullHandle empty tree; return appropriate default (0, [], null)
Single node treeLeaf node is both root, left, and right boundary
Skewed tree (all left or right)Don’t assume O(log N) depth; stack may overflow
Negative values in nodesPath sum with all negatives; don’t discard negative paths
Duplicate values in BSTClarify how duplicates are handled (left or right subtree)
Integer overflowSum of path may exceed Number.MAX_SAFE_INTEGER in JS

MistakeFix
❌ Using queue.shift() for BFS in JavaScript (O(N) per operation!)Use a proper queue (two-stack or pointer trick) for performance-critical code. For interviews, shift() is usually acceptable; clarify.
❌ Confusing height (edges) vs depth (edges from root)Know which definition the problem uses
❌ Assuming a BST is always balancedIt may be skewed! Test worst-case scenarios
❌ Not handling null children in traversalAlways check for null before accessing node.left or node.right

  • 📝 Draw the tree on paper before coding. Trace your algorithm step by step.

  • 🧪 Test 4 cases: null, single node, two nodes, normal tree.

  • 🪄 Use sentinel values like -Infinity for range validation in BST problems.

  • 🧩 Combine techniques: Hard problems = “find height” + “return additional state”.

  • 🔄 Return multiple values from recursion when needed (e.g., both height AND balanced status).

  • 🌲 Think recursively: Every subtree is itself a valid tree — if you solve for the root, you’ve solved for all nodes.


A tree is just a root node connected to smaller trees (subtrees).

  • To traverse, handle the root, then let recursion handle the subtrees.
  • To compute a property (height, sum, etc.), compute for children, combine at root.
  • To modify the tree, return the new subtree root from recursion — parent will reconnect.

Master the base case (null), and everything else follows naturally.