How to print path of a binary tree after adding every node (branches of a tree have values 0 and 1)? For example, I have this binary tree:
NODE1 has two children, NODE2 and NODE3. Branch from NODE1 to NODE2 is 0, and from NODE1 to NODE3 is 1.
From NODE2 to NODE4 is 1.
From NODE4 to NODE5 is 0.
How to print a full path 0-1-1-0.
I have written this function (don't have a complete code, so can't compile it)
void pathToNode(QUEUE* queue, NODE* node, NODE* root, int* found)
{
if(root == NULL)
{
return;
}
if(root == node)
{
*found = 1;
return;
}
inputElementInQueue(queue, 0);
pathToNode(queue, node, root -> left, found);
if(*found == 0)
{
returnFirstElementOfQueue(queue);
inputElementInQueue(queue, 1);
pathToNode(queue, node, root -> right, found);
}
if(*found == 0)
{
returnFirstElementOfQueue(queue);
}
}
Is this right?
Thanks for replies.