Binary tree
easy 18 minA binary tree is a hierarchy of nodes where each node has at most two children, left and right, hanging from a single root.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 10
The BST has 4 at the root, 2 and 6 as its children, and 1, 3, 5, 7 as leaves. What is its inorder traversal?
What is the depth of the root, and the height of a leaf, counting edges?
What is the largest number of nodes a binary tree of height 3 can have?
A binary tree has 100 nodes. What is the smallest height it can have?
Which statement is always true?
In a 0-based array layout, where are the children of the node at index 4?
What is the postorder traversal of the tree 1 (root), 2 and 3 (children), 4 and 5 under 2?
You need the smallest depth at which a leaf appears. Which traversal fits best?
What is the worst-case extra space of a recursive traversal on n nodes?
A full binary tree has 10 internal nodes. How many leaves does it have?
Pick an answer to see why.
Not quite. Inorder is left, node, right; in a BST that means smallest to largest. The other options are level order, postorder and preorder.
Not quite. Depth counts edges down from the root, so the root is 0. Height counts edges down to the deepest leaf, so a leaf is 0.
Not quite. Levels 0 to 3 hold at most 1 + 2 + 4 + 8 = 15 = 2^(3+1) − 1 nodes.
Not quite. ⌊log₂ 100⌋ = 6. Height 6 allows up to 127 nodes, while height 5 allows only 63.
Not quite. A perfect tree has every level full, which meets the complete definition. Full trees can have gaps, complete trees can have a partial last level, and balanced trees can have holes.
Not quite. 2 · 4 + 1 = 9 and 2 · 4 + 2 = 10. Its parent is (4 − 1) // 2 = 1.
Not quite. Left subtree first (4, 5, then 2), then the right subtree (3), then the root 1.
Not quite. BFS reaches levels in order, so the first leaf it meets is the shallowest one, and it can stop right there.
Not quite. Space is O(h), and a degenerate tree has height n − 1. It is O(log n) only when the tree is balanced.
Not quite. In a full tree leaves = internal + 1, so 11 leaves and 21 nodes in total.
Flashcards
Say your answer, then reveal it.
Card 1 of 12
What makes a tree binary?
Every node has at most two children, called left and right, and the two are distinguished: swapping them gives a different tree.
Say your answer, then reveal it.
Depth vs height of a node?
Depth: edges from the root down to it (root = 0). Height: edges on the longest path from it down to a leaf (leaf = 0).
Say your answer, then reveal it.
How many edges does a tree with n nodes have?
n − 1. Every node except the root has exactly one edge to its parent.
Say your answer, then reveal it.
Most nodes on level k? In a tree of height h?
2^k on level k; 2^(h+1) − 1 in total, reached only by a perfect tree.
Say your answer, then reveal it.
Smallest and largest height for n nodes?
Smallest ⌊log₂ n⌋ (complete tree), largest n − 1 (a chain).
Say your answer, then reveal it.
Full vs complete vs perfect?
Full: 0 or 2 children everywhere. Complete: all levels full except the last, filled from the left. Perfect: every level full. Perfect implies both others.
Say your answer, then reveal it.
What is a balanced binary tree?
At every node the left and right subtree heights differ by at most 1, so the height stays O(log n).
Say your answer, then reveal it.
Array layout: where are the children and parent of index i?
Children at 2i + 1 and 2i + 2, parent at (i − 1) // 2, using 0-based indexes.
Say your answer, then reveal it.
The three DFS orders?
Preorder N-L-R, inorder L-N-R, postorder L-R-N. The name tells you where the node goes.
Say your answer, then reveal it.
Which traversal uses a queue?
Level order (BFS). The three depth-first orders use a stack or recursion.
Say your answer, then reveal it.
Why does inorder of a BST come out sorted?
Everything in the left subtree is smaller and everything in the right is larger, so left, node, right visits values in ascending order.
Say your answer, then reveal it.
Time and space of a traversal?
O(n) time. O(h) space for DFS (log n balanced, n skewed); O(w) for BFS, w the widest level.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.