Trees (Binary Tree & BST)

Use for traversals (in/pre/post/level), path sums, LCA, BST properties.

What this pattern is

Traverse trees with DFS (inorder, preorder, postorder) or BFS (level order) to compute properties. Use BST ordering to validate and to bound recursion.

Exercise: TreeNode

Your Solution

Exercise: inorder

Task: Return the inorder traversal of a binary tree.
Example: For BST, inorder returns nodes in sorted order.
Edge cases: skewed trees, single node, empty tree.

Your Solution

Exercise: isValidBST

Task: Determine if a binary tree is a valid BST using range bounds.
Edge cases: duplicates on left/right, large negative/positive values, degenerate chains.

Your Solution

Exercise: levelOrder

Task: Return the level order traversal (BFS layers) of a binary tree as a list of levels.
Example: For [3,9,20,null,null,15,7][[3],[9,20],[15,7]].

Your Solution

Exercise: postorder

Task: Return the postorder traversal of a binary tree.
Use case: delete/free operations, computing heights from leaves up.

Your Solution

Exercise: preorder

Task: Return the preorder traversal of a binary tree.
Use case: serialize structure, root-first computations.

Your Solution