7.7 Exercises¶
7.7.1 Concept Review¶
1. Complete, Full, and Perfect Binary Trees¶
The following two arrays represent binary trees in level order, where None marks an empty position:
- Tree A:
[1, 2, 3, 4, 5, 6] - Tree B:
[1, 2, 3, None, None, 6, 7]
- Which tree is a complete binary tree?
- Which tree is a full binary tree, meaning every non-leaf node has two children?
- Is either tree a perfect binary tree? Explain the reason for each tree.
Answer
-
Tree A is a complete binary tree. Only its lowest level is not full, and the nodes on that level occupy consecutive positions from left to right. Tree B is not complete because there are empty positions on the left of the lowest level while nodes still appear on the right.
-
Tree B is a full binary tree: nodes 1 and 3 each have two children, and all other nodes are leaves. Tree A is not full because node 3 has only one child, its left child 6.
-
Neither tree is perfect because the lowest level of each tree is not completely filled.
2. Three Traversal Orders for the Same Tree¶
Store the array [1, 2, 3, 4, 5, 6, 7] in level order in a complete binary tree.
- Draw the tree.
- Write its preorder, inorder, and postorder traversal sequences.
- In the inorder sequence, which parts of the tree correspond to the subsequences to the left and right of root node 1?
Answer
-
The tree is:
-
The preorder traversal is
1, 2, 4, 5, 3, 6, 7; the inorder traversal is4, 2, 5, 1, 6, 3, 7; the postorder traversal is4, 5, 2, 6, 7, 3, 1. -
The sequence
4, 2, 5to the left of root node 1 is the inorder traversal of the left subtree; the sequence6, 3, 7to its right is the inorder traversal of the right subtree.
3. Compare Two Binary Search Trees¶
Insert each of the following sequences from left to right into an empty binary search tree:
- Sequence A:
[4, 2, 6, 1, 3, 5, 7] - Sequence B:
[1, 2, 3, 4, 5, 6, 7]
- For each tree, write the nodes visited when searching for 7.
- If height is measured by the number of edges from the root node to the farthest leaf node, what is the height of each tree?
- Based on the first two questions, is searching for 7 equally efficient in the two trees? Explain using the trees' shapes and search paths.
Answer
-
In the tree built from Sequence A, the search path is
4 → 6 → 7. In the tree built from Sequence B, the search path is1 → 2 → 3 → 4 → 5 → 6 → 7. -
Every level of the first tree is full, and its height is 2. The second tree has only right children, and its height is 6.
-
Searching for 7 is not equally efficient in the two trees. The insertion order changes the shape and height of a binary search tree. The search visits only 3 nodes in the first tree but all 7 nodes in the second. The taller the tree, the more nodes may need to be compared along a path in the worst case.
7.7.2 Programming Exercises¶
1. Maximum Depth of a Binary Tree¶
You are given the root node root of a binary tree. Each node contains an integer value and references to its left and right children.
The maximum depth is the number of nodes on the path from the root node to the farthest leaf node. Return the maximum depth of the tree; the maximum depth of an empty tree is 0. Use recursion.
Hints
- Depth is measured by the number of nodes in this exercise, so a tree containing only a root node has a maximum depth of 1
- Let the recursive function return the maximum depth of the subtree rooted at the current node
- Return 0 for an empty node; for a nonempty node, return max(depth(left), depth(right)) + 1
2. Traverse a Binary Tree Level by Level¶
Given the root node root of a binary tree, use a queue to visit all nodes level by level from top to bottom and from left to right within each level.
Return a two-dimensional array: the first subarray stores the values at the root's level, the second stores the values at the next level, and so on. If the tree is empty, return an empty array.
Hints
- Level-order traversal visits earlier enqueued nodes first, so use a queue
- At the beginning of each round, all nodes currently in the queue belong to the same level
- First record the queue's length, then remove exactly that many nodes and enqueue their children
3. Kth Smallest Element in a Binary Search Tree¶
A binary search tree contains n nodes with distinct values.
If all node values are arranged from smallest to largest, their positions are numbered starting from 1.
Given the root node root and an integer k satisfying 1 <= k <= n, return the value at position k.
Find the answer directly during an inorder traversal rather than collecting all node values first.
Hints
- An inorder traversal of a binary search tree visits node values from smallest to largest
- Inorder traversal processes the left subtree, the current node, and then the right subtree; increment the count when visiting the current node
- When the count first equals k, the current node's value is the answer, so no further traversal is needed