I'm trying to solve this recurrence of a function of a binary tree with a recursive tree. But I can't find any pattern to solve it.
This function calculates both the height and if its a balanced tree.
template <typename T>
std::pair<bool, int> balanced_height(const BinTree<T> &tree) {
if (tree.empty()) {
return {true, 0};
} else {
auto [bal_left, height_left] = balanced_height(tree.left());
auto [bal_right, height_right] = balanced_height(tree.right());
bool balanced = bal_left && bal_right && abs(height_left - height_right) <= 1;
int height = 1 + std::max(height_left, height_right);
return {balanced, height};
}
}
And the recurrence, $n$ is the number of tree nodes. I think that $n_l$ and $n_r$ should decrease in each call by 1.
$$ T(n) = \begin{cases} T(n_l)+T(n_r) + 1 & \text{if } n \gt 0, \\ 1 & n = 0 \\ \end{cases} \\ \text{Given that }n_l+n_r+1=n $$