Trees and Binary Trees
Lesson 14 of 20Walk a binary tree level by level with a queue, then recurse through it inorder — with Java code that prints it.
Meet the binary tree
Trees trade the linear world of arrays for hierarchy: one root, branches, leaves. A binary tree caps every node at two children — and traversal order suddenly matters a lot.
Count the edges
A tree with n nodes always has exactly n - 1 edges — connected and cycle-free by definition.
Binary node anatomy
Each node holds a value plus left and right references. Null links mark the boundaries: n nodes carry n + 1 of them.
Vocabulary
Root at the top, leaves at the bottom (zero children), height = longest root-to-leaf edge count. A lone node has height 0.
Four ways to walk
Preorder (node-left-right), inorder (left-node-right), postorder (left-right-node) use recursion; level order uses a queue.
Where you meet it
The DOM, file systems, expression parsing and autocomplete all live in trees — BSTs and heaps are the next step.
Level order live
A queue sweeps the tree top to bottom, left to right — every node exactly once.
Level order starts at the root. A queue holds the discovered-but-unvisited nodes — 50 is up first.
Inorder flow
Recursive traversal is three moves: go left, visit, go right — null is the escape hatch.
Walkthrough
Recursive inorder prints a subtree — then recursion counts nodes without any loop.
1public class Traversals {2static class Node {3int val;4Node left, right;5Node(int v) { val = v; }6}78static void inorder(Node n) {9if (n == null) return;10inorder(n.left);11System.out.println(n.val);12inorder(n.right);13}1415public static void main(String[] args) {16Node root = new Node(50);17root.left = new Node(27);18root.right = new Node(74);19root.left.left = new Node(13);20inorder(root);21}22}
root = 50 — the top of the tree.
Inorder in motion
Left, node, right — on a BST this order comes out perfectly sorted.
Inorder(50): the rule is left subtree, then the node, then the right subtree — 50 waits.
Predict the output
Height by recursion — Math.max picks the deeper subtree at every node.
1public class Predict {2static class Node {3int val;4Node left, right;5Node(int v) { val = v; }6}78static int height(Node n) {9if (n == null) return 0;10return 1 + Math.max(height(n.left), height(n.right));11}1213public static void main(String[] args) {14Node a = new Node(1);15a.left = new Node(2);16a.left.left = new Node(4);17a.right = new Node(3);18System.out.println(height(a));19}20}
Hint: height(null) is 0 and every node adds 1 for its own edge. The longest root-to-leaf path wins.
Concept check
Click a term, then click its meaning to lock the pair.
Key takeaways
A tree with n nodes has n - 1 edges and n + 1 null child links.
Every recursive traversal needs the null base case — it is the floor of the recursion.
Inorder = left, node, right; preorder = node first; postorder = node last.
Level order is the odd one out: a queue instead of recursion.
Height counts edges on the longest root-to-leaf path — recursion with Math.max.
Most tree problems are one-liners once you trust 1 + f(left) + f(right).