Binary Tree Fundamentals


Binary Tree Fundamentals

Simon
Before we dive into the code examples, let's make sure we're solid on the fundamentals. We're talking binary trees, and there are three key terms to describe them: full, complete, and perfect.

Hope
Right. And I think the easiest way to remember them is with those shapes. A full tree is one where every node has either zero or two children — never just one. So leaves have no children, and internal nodes always have both left and right.

Simon
Exactly. So if you see a node with a single child, it's not full. Then you've got a complete tree — and that's the shape used for binary heaps. The rule is we fill from top to bottom, left to right, with no gaps.

Hope
So if you picture nodes being added row by row, each row fills up left to right before you start a new row. If you skip a spot in the middle, it's no longer complete.

Simon
And then a perfect binary tree is the most restrictive: all leaves are on the same level, and every internal node has exactly two children. It's both full and complete. Think of a full tournament bracket.

Hope
That makes sense. Now with those definitions down, let's talk traversals. The three classic ones are pre-order, in-order, and post-order. The trick is in the prefixes: pre, in, and post tell you when you process the current node relative to its subtrees.

Simon
Exactly. Pre-order means visit the node first, then the left subtree, then the right. In-order means left, then node, then right. Post-order means left, then right, then node. The left and right always go in that order — the prefix just moves the node visit.

Hope
And the best way to internalize this is with recursion. Every node follows the same policy, but the state on the call stack changes as we move through the tree. Let's walk through the tree example from the video: root is 10, left child 7, right child 11, and so on.

Simon
For pre-order, we start at 10. Policy: visit node first, then left, then right. So we print 10, then recurse left to 7. At 7, same policy: print 7, go left to 6, print 6, then left to 1, print 1. Then 1 has no left or right, so we return to 6, then 6's right is null, so return to 7, then 7's right is 8, and so on.

Hope
Right. And you keep going until you've visited every node exactly once. The order ends up being 10, 7, 6, 1, 8, 9, 11, 20, 14, 22. In-order is different: left first, then node, then right. So from 10, we go left all the way to 1. Print 1, return to 6, print 6, then 6's right is null, return to 7, print 7, then go right to 8, etc.

Simon
And that in-order traversal has a special property when the tree is a binary search tree: it prints the values in sorted order. In our example, the tree is indeed a BST — everything left of 10 is less, everything right is greater. So in-order gives 1, 6, 7, 8, 9, 10, 11, 14, 20, 22 — increasing order.

Hope
That's a key insight. Then post-order: left, right, then node. So from 10, left all the way to 1, then 1's left and right are null, print 1, return to 6, print 6 (after its right null), return to 7, go to 8, etc. The root is printed last.

Simon
Post-order is useful when you need to delete a tree or free memory — you process children before the parent. And for all these traversals, the time complexity is O(n), where n is the number of nodes, because we visit each node exactly once.

Hope
So the key takeaway is: the traversal names tell you when to handle the node. Pre means before children, in means between children, post means after. And recursion is just applying that same policy at every node, with the call stack keeping track of where you are.

Simon
And that's the foundation. Once you're comfortable with these recursive traversals, you can adapt them for iterative solutions using your own stack, or explore breadth-first with a queue. But understanding these basics is what makes harder tree problems click.

Ask anything
We've read this whole study set, so questions about it get the best answers.