data-structures · intermediate · ~15 min

Height of a binary tree

Recurse over a binary tree.

Challenge

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.

Starter code

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.