#include struct node { int nchilds; struct node * childs; int depth; }; int dfs(struct node* root, int nnodes) { //If nnodes unknown double dynamically and keep track of size and usage struct node **stack = malloc(sizeof(struct node *)*nnodes), *current; int i; int bottom = 0; //Queue the root node root -> depth = 0; stack[bottom++] = root; while(bottom) { //Extract last inserted node current = stack[bottom--]; //Push the childs on the stack for (i = 0; i < current->nchilds; i++) { stack[bottom++] = current->childs[i]; current->childs[i]->depth = current->depth+1; } } }