I am trying to create a binary tree of processes in which each parent is connected to its two children through pipes.

Problem: Parent process A creates two processes(B and C) and two pipes, one for each process. Their file descriptors are stored into fd. In the second iteration, B spawns its two children. B overwrites the file descriptors stored into fd with the new pipe's file descriptors. After spawning my n number of levels, the only pipes that remain are the leaf nodes to their parents(up one level). 

I've tested this theory and the only things being communicated are between at the bottom of the tree, leaf to up-one-level. I have to make it so the leaf nodes can communicate all the way up the tree to the master process.

I am new to pipes so I could be off about my explanation.

Is my understanding correct, and also what should I do to fix this issue?

Sample Code:

```
#define READ 0
#define WRITE 1

int fd[2][2];

void
spawnChildren(int levels)
{
    if(levels == 0)
       return;

    pipe(fd[0]);
    //spawns 2 children at a single parent
    int pid = fork();
    //parent
    if(pid > 0)
    {
        close(fd[0][WRITE]);
        pipe(fd[1]);
        int pid2 = fork();
        //child B
        if(pid2 == 0)
        {
            close(fd[1][READ]);
            spawnChildren(levels-1);
            return;
        }
        //parent
        else
            close(fd[1][WRITE]);
    }
    //child A
    else
    {
        close(fd[0][READ]);
        spawnChildren(levels-1);
        return;
    }
}

```