Binary Tree Visualizers

29

Step through every Binary Tree problem with animated runtime state, source highlighting, and curated test cases. Built for coding-interview prep and durable algorithmic intuition.

Balanced Binary Tree
Bottom-up DFS checking if subtree heights differ by at most 1. Early termination on imbalance.
Binary Tree
Binary Tree Inorder Traversal
Recursive DFS visiting left, then root, then right. See how in-order produces sorted output for BSTs.
Binary Tree
Binary Tree Inorder Traversal - Iterative
Iterative inorder using curr pointer + stack. Two-phase loop: go left (push), then pop-record-go right.
Iterative Stack
Binary Tree Level Order Traversal
BFS level-order traversal using a queue. Watch nodes get visited level by level with queue state tracked in real time.
Binary Tree
Binary Tree Level Order Traversal - Recursion
DFS recursion with a level parameter to slot each node into the correct sub-array — no queue needed.
DFS with Level Parameter
Binary Tree Maximum Path Sum
Postorder DFS computing max gain from each subtree while tracking the global maximum path sum.
Binary Tree
Binary Tree Postorder Traversal
Recursive DFS visiting left, then right, then root. Children are always processed before their parent.
Binary Tree
Binary Tree Postorder Traversal - One Stack
Iterative postorder with a single stack. Uses curr and lastVisited pointers to avoid revisiting the right subtree.
Iterative Stack
Binary Tree Postorder Traversal - Two Stacks
Iterative postorder using two stacks. Phase 1: fill stack2 in reverse-postorder via stack1. Phase 2: pop stack2 for correct order.
Iterative Stack
Binary Tree Preorder Traversal
Recursive DFS visiting root, then left, then right. Watch the call stack and result array build in real time.
Binary Tree
Binary Tree Preorder Traversal - Iterative
Iterative DFS using an explicit stack. Push root, then loop: pop → record → push right → push left.
Iterative Stack
Binary Tree Right Side View
BFS collecting the last node of each level. See which nodes are visible from the right side of the tree.
Binary Tree
Binary Tree Zigzag Level Order Traversal
BFS with alternating direction per level. Watch the zigzag pattern build as levels alternate left-to-right and right-to-left.
Binary Tree
Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder arrays, recursively construct a binary tree. Preorder gives the root, while inorder splits left and right subtrees.
Trees / Recursion
Count Good Nodes in Binary Tree
DFS tracking the max value on the path from root. A node is good if its value >= max so far.
Binary Tree
Diameter of Binary Tree
DFS computing height while tracking max left+right height across all nodes to find the longest path.
Binary Tree
Invert Binary Tree
Recursive DFS swapping left and right children at every node. Watch the tree mirror itself.
Binary Tree
Lowest Common Ancestor of a Binary Tree
DFS returning p or q when found, identifying the LCA where both subtrees return non-null.
Binary Tree
Maximum Depth of Binary Tree
DFS recursion computing 1 + max(left, right) depth. Watch depth values propagate back up the tree.
Binary Tree
Maximum Depth of Binary Tree - Top Down
Top-down DFS carrying depth as a parameter. Update global max at each node — preorder style.
Top-Down DFS
Path Sum
DFS exploring root-to-leaf paths with a running sum. Backtrack when a path doesn't reach the target.
Binary Tree
Path Sum - Top Down
Top-down DFS carrying a running sum. Check newSum === target at leaves. An 'ans' flag locks in the result.
Top-Down DFS
Populating Next Right Pointers in Each Node
BFS level-by-level connecting each node's next pointer to the next node in the same level.
Binary Tree
Same Tree
Recursive DFS comparing two trees node by node. Both structure and values must match.
Binary Tree
Serialize and Deserialize Binary Tree
Convert a binary tree to a string using level-order traversal (BFS), and reconstruct the exact same tree from the string.
Trees / BFS
Subtree of Another Tree
For each node in the main tree, check if the subtree rooted there matches the target tree.
Binary Tree
Subtree of Another Tree (Serialization)
Pre-order serialize both trees with null markers, then check if subRoot's string is a substring of root's string.
Binary Tree / Serialization
Symmetric Tree
Recursive mirror check comparing left.left with right.right and left.right with right.left.
Binary Tree
Symmetric Tree - Iterative
Iterative BFS using a queue: enqueue mirror pairs, dequeue two at a time, validate, and push children in mirror order.
BFS / Queue