Trees and Binary Trees
Lesson 14 of 20Watch an inorder walk mark every node in visit order — then see why that same order prints a BST sorted.
The big picture
A tree is the data structure of hierarchy — one root, branches, leaves. Cap every node at two children and one tiny recursive function can answer almost any question about it.
Nodes and edges
A tree stores data in nodes joined by edges. One node is the root; nodes with no children are leaves. A 7-node binary tree has exactly 6 edges.
The binary rule
Every node holds at most two children, named left and right. In C++ that is just two pointers: node->left and node->right.
Depth and height
Depth counts links from the root down to a node; height counts the longest root-to-leaf path in nodes. A lone node has depth 0 and height 1.
Three DFS orders
Inorder = left, node, right. Preorder = node, left, right. Postorder = left, right, node. Same tree, three different stories.
Recursion fits perfectly
Every subtree is itself a tree, so height(), sum() and count() are all 5-line recursive functions with one base case: the empty tree.
Where you meet them
File systems, org charts, the DOM, autocomplete tries and expression parsers are all trees doing their day jobs.
Inorder in motion
Left subtree, then the node, then the right subtree — watch the visit order stack up.
A 7-node binary tree. The inorder rule: finish the entire left subtree first, then visit the node, then the right subtree.
The recursion flow
Every traversal is this one tiny loop — the call stack remembers where to return.
Walkthrough
Inorder traversal on a real 3-node tree — then the same recursion computes height.
1#include <iostream>2using namespace std;34struct Node {5int val;6Node* left;7Node* right;8};910void inorder(Node* r) {11if (!r) return;12inorder(r->left);13cout << r->val << " ";14inorder(r->right);15}1617int main() {18Node a{1, nullptr, nullptr};19Node c{3, nullptr, nullptr};20Node root{2, &a, &c};21inorder(&root);22return 0;23}
Node a — a leaf holding 1. struct Node packs one int plus left/right pointers.
Preorder in motion
Same tree, new rule: the node comes FIRST — parents before children everywhere.
The same 7-node tree, but preorder runs on a different rule: visit the node first, then left, then right.
Predict the output
Both recursive calls run before the print — trace which value hits cout first.
1#include <iostream>2using namespace std;3struct Node { int v; Node* l; Node* r; };45void walk(Node* n) {6if (!n) return;7walk(n->l);8walk(n->r);9cout << n->v;10}1112int main() {13Node a{4, nullptr, nullptr}, b{5, nullptr, nullptr};14Node root{2, &a, &b};15walk(&root);16return 0;17}
Hint: The parent prints after both of its recursive calls — children before parents. Which of the three orders is that?
Concept check
Click a term, then click its meaning to lock the pair.
Key takeaways
A binary node is one value plus two pointers: left and right.
nullptr children mark leaves — that is the base case every tree recursion needs.
Inorder = left, node, right; preorder = node first; postorder = node last.
Inorder on a binary search tree walks out the values in ascending order.
Height of an empty tree is 0, a leaf is 1, and every level up adds 1.
Count, sum, height — all are 5-line recursions because every subtree is itself a tree.