12.1. Exercises#

Many of these exercises are taken from past exams of ECE 244 Programming Fundamentals courses at University of Toronto. The solutions are provided in the answer boxes.

Headings in this page classify the exercises into different categories: [Easy], [Intermediate], and [Challenging]. I suggest you start by easy exercises and work your way up to the challenging ones.

Question 10 in Fall 2021 Final Exam [Easy]

Consider the following binary tree. The keys of the nodes are shown below inside the nodes.

Tree Traversal
  1. What is the preorder traversal of the tree?

  2. What is the inorder traversal of the tree?

  3. What is the postorder traversal of the tree?

Question 9 in Fall 2022 Final Exam [Intermediate]

A binary tree is symmetric if the root node’s left subtree is exactly a mirror reflection of the right subtree. For example, the tree on the left is symmetric, but the tree on the right is not.

This tree is symmetric

Fig. 12.1 This tree is symmetric#

This tree is not symmetric

Fig. 12.2 This tree is not symmetric#

Now, you are asked to complete the function is_symmetric. It should return true if and only if the root is a symmetric tree. You should fill out the recursive helper function is_symmetric_helper to make is_symmetric work as expected. Hint: you should write less than 15 lines of code.

/* Definition of the tree node */
class TreeNode {
  int data;
  TreeNode* left;
  TreeNode* right;

/* Recursive helper */
bool is_symmetric_helper(TreeNode* left, TreeNode* right);

bool is_symmetric(TreeNode* root) {
  if (root == NULL) {
    return true;
  return is_symmetric_helper(root->left, root->right);

Complete the helper function is_symmetric_helper.

In progress!