BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ Algorithms and data structures

Java language

Trees and tree traversal

A tree is a hierarchy of nodes where each node has children (a binary tree has at most a left and a right child).

Learn these first

At a glance

Mental model

A family tree or a folder structure: every person or folder has smaller branches below it, and processing "everything below here" means doing the same thing for each child.

What happens, in order
  1. 1Node: class TreeNode { int val; TreeNode left, right; }A tree is a reference to its root; empty subtrees are null.
  2. 2Depth-first traversals: preorder (node, left, right), inorder (left, node, right), postorder (left, right, node)Inorder on a binary search tree visits the values in sorted order.
  3. 3Breadth-first (level order) uses a queue: add the root, then repeatedly poll a node and add its children
  4. 4Height: 1 + Math.max(height(left), height(right)) with 0 for nullBinary search tree: values less than the node go left, larger go right, so search, insert and delete are O(height).
Remember
Watch out
Forgetting the null base caseConfusing preorder, inorder and postorderAssuming a BST is balancedModifying the tree while traversing it
Example (highlighted lines explain it)
import java.util.*;public class Trees {  static class TreeNode { int val; TreeNode left, right; TreeNode(int v) { val = v; } }  static int maxDepth(TreeNode node) {    if (node == null) return 0;    return 1 + Math.max(maxDepth(node.left), maxDepth(node.right));  }  static void inorder(TreeNode node, List<Integer> out) {    if (node == null) return;    inorder(node.left, out);    out.add(node.val);    inorder(node.right, out);  }  static TreeNode insertNode(TreeNode root, int v) {    if (root == null) return new TreeNode(v);    if (v < root.val) root.left = insertNode(root.left, v); else root.right = insertNode(root.right, v);    return root;  }}
  1. line 3 uses TreeNode, the core of Trees and tree traversal
  2. line 4 uses TreeNode, the core of Trees and tree traversal
  3. line 8 uses TreeNode, the core of Trees and tree traversal
  4. line 14 uses TreeNode, the core of Trees and tree traversal
  5. line 15 uses TreeNode, the core of Trees and tree traversal
Read the full explanation (about 1 min)

A tree is a hierarchy of nodes where each node has children (a binary tree has at most a left and a right child). Many tree algorithms are three lines of recursion: handle the node, then recurse into the children.

A mental model

A family tree or a folder structure: every person or folder has smaller branches below it, and processing "everything below here" means doing the same thing for each child.

How it works

Node: class TreeNode { int val; TreeNode left, right; }. A tree is a reference to its root; empty subtrees are null.

Depth-first traversals: preorder (node, left, right), inorder (left, node, right), postorder (left, right, node). Inorder on a binary search tree visits the values in sorted order.

Breadth-first (level order) uses a queue: add the root, then repeatedly poll a node and add its children.

Height: 1 + Math.max(height(left), height(right)) with 0 for null. Binary search tree: values less than the node go left, larger go right, so search, insert and delete are O(height).

Going deeper

A balanced tree has height about log n, so operations are O(log n); a degenerate tree (inserting sorted data into a plain BST) is a linked list with O(n) operations. Self-balancing trees (red-black as in TreeMap, AVL) prevent that.

Recursion depth equals the tree height; for very deep trees use an explicit Deque stack.

Always handle null first: the base case of every tree recursion is "if node is null, return".

Under the hood

A node is a heap object with two references, so a tree of n nodes uses n objects. Heaps (PriorityQueue) store a complete binary tree in an array with the children of i at 2i+1 and 2i+2, avoiding node objects.

Common mistakes

  • Forgetting the null base case.
  • Confusing preorder, inorder and postorder.
  • Assuming a BST is balanced.
  • Modifying the tree while traversing it.

How Unravel recognises it

When you paste code, Unravel looks for these signs of trees and tree traversal. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is Trees and tree traversal?

A tree is a hierarchy of nodes where each node has children (a binary tree has at most a left and a right child). Many tree algorithms are three lines of recursion: handle the node, then recurse into the children.

What are the most common mistakes with Trees and tree traversal?

Forgetting the null base case. Confusing preorder, inorder and postorder. Assuming a BST is balanced. Modifying the tree while traversing it.

How can I see Trees and tree traversal in real code?

Paste your own Java into Unravel and it will point out where trees and tree traversal appears, explain how it works at the detail level you choose, and draw the flow of the code around it.