My task is to calculate depth of each node and store it in 'depth' given in Node class. But I don't know how I should approach this task. I was looking for some example in the internet but haven't found any appropriate to my task. This is the code of my given Node class:
Node
{int value; Node left, right; int depth;}
I thought I could use a similar method to counting height of a tree but it didn't work out. Any help?
void updateDepth(Node node, int depth)
{
if (node != null)
{
node.depth = depth;
updateDepth(node.left, depth + 1); // left sub-tree
updateDepth(node.right, depth + 1); // right sub-tree
}
}
Call with updateDepth(root, 0);