Adobe Interview Question
Software Engineer / Developersdft is a soln but without dft recursive simple solution exists.
typedef struct node * NODE;
int depth ( NODE nd )
{
return nd?1 + max ( depth ( nd ->left ) , depth ( nd ->right ) ):-1;
}
for height of tree just change -1 in code to 0;
int maxdeepth(Node *root)
{
if (root == NULL)
return 0
}
else
{
return (1 + max(maxdeepth(root->left),maxdeepth(x->right)));
}
This will result in the height of the binary tree.
if ( root == NULL ) return -1;
now it will work.
Do a depth first traversal and keep on increasing parents height as rolling out of the recursion.
- Anonymous August 04, 2010