data-structures · intermediate · ~15 min
Recurse over a binary tree.
With typedef struct tnode { int v; struct tnode *l, *r; } tnode_t;, implement int tree_height(tnode_t *root) returning the height (number of nodes on the longest root-to-leaf path). Empty tree has height 0.
typedef struct tnode { int v; struct tnode *l, *r; } tnode_t;
int tree_height(tnode_t *root) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.