Recursion for the ACTUAL maximum depth of a BT.
Hi,
I wrote the following function to determine the maximum depth of a binary tree, but it feels somewhat cumbersome to me. I'd truly appreciate it if you could help me improve it (make it shorter if possible and more elegant), albeit it works great:
Code:
int findMaxDepth(Node * tree, int curr){
int left, right;
if (!tree)
return 0;
right = findMaxDepth(tree->right, curr + 1);
left = findMaxDepth(tree->left, curr + 1);
curr = (curr > right) ? curr : right;
return (left > curr ? left : curr);
}
Any insightful comments will be highly appreciated! :-)
PS All the variations thereof which I saw online, and in other sources, find the maximum depth + 1, not the ACTUAL maximum depth. The above function yields the actual depth as desired.