Binary Tree

Guide

Basic Traversal 3
Easy warm-ups 6
BST variations 5
Mediums 7
Waste 2

Basic traversals

‼️In-order

Recursion Easy

O(n) time and space
Pasted image 20250503111412.png
Pasted image 20250503110740.png

Iterative

Pre-order

Recursion

Pasted image 20250503112339.png

Iteration - The usual stack thing

Post-Order

Recursion

Pasted image 20250503113115.png

Iteration

single-stack iterative post-order traversal
reversing output of a DFS which processes right child first than left child gives back post order
Pasted image 20250503114429.png

function postorderTraversal(root) {
  const result = [];
  const stack = [];

  if (root) stack.push(root);

  while (stack.length > 0) {
    const node = stack.pop();
    result.push(node.val);

    if (node.left) stack.push(node.left);
    if (node.right) stack.push(node.right);
  }

  return result.reverse();
}

Easy warm-ups(6):


BST Variations (5)

Medium Probs (7)

Waste Problems (2)




# Other practice questions

1. Find minimum value in BST

```js
	function findMin(node) {
	  while (node.left) {
	    node = node.left;
	  }
	  return node.val;
	}

  1. Find max value in BST

	function findMax(node) {
	  while (node.right) {
	    node = node.right;
	  }
	  return node.val;
	}