diff --git a/lectures/18_trees_I/README.md b/lectures/18_trees_I/README.md index c1a6f12..a17a8a7 100644 --- a/lectures/18_trees_I/README.md +++ b/lectures/18_trees_I/README.md @@ -104,10 +104,34 @@ public: 1. Write a templated function to find the smallest value stored in a binary search tree whose root node is pointed to by p. -2. Write a function to count the number of odd numbers stored in a binary tree (not necessarily a binary search +```cpp +template +const T& find_smallest (TreeNode * root) +{ + if(root->left == NULL) + { + return root->value; + } + return find_smallest(root->left); +} +``` + +3. Write a function to count the number of odd numbers stored in a binary tree (not necessarily a binary search tree) of integers. The function should accept a TreeNode pointer as its sole argument and return an integer. Hint: think recursively! +```cpp +int count_odd (TreeNode* int) +{ + if(node == nullptr) + { + return 0; + } + return abs(node->value % 2) + count_odd(node->left) + count_odd(node->right); +} + +``` + ## 18.7 ds_set and Binary Search Tree Implementation - A partial implementation of a set using a binary search tree is provided in this [ds_set_starter.h](ds_set_starter.h). We will continue to study this implementation in Lab 10 & the next lecture.