I just want to know why my code will crash when the deepth is odd, please comment me if you are Interested in this Bug.

**UPDATED**

clean code with comments.

```
class Solution
{
public:
void connect(TreeLinkNode *root)
{
//initial
int maxDeep = deepCounter(root);
if(maxDeep == 0)
{
return;
}
//get the counts of each layer
int* lengthOfEachFloor = ElementCounter(maxDeep);
TreeLinkNode** upFloor = new TreeLinkNode*[1];
upFloor[0] = root;
upFloor[0]->next = NULL;
if(2 <= maxDeep)
{
doConnect(upFloor, 2, maxDeep, lengthOfEachFloor);
}
delete[] lengthOfEachFloor;
}
//connect each layer
void doConnect(TreeLinkNode** upFloor, int deep, int maxDeep, int* lengthOfEachFloor)
{
//upNum saved the counts of upper layer
int upNum = lengthOfEachFloor[deep - 1];
//nowNum saved the counts of current layer
int nowNum = lengthOfEachFloor[deep];
//nowFloor saved the nodes of current layer
//When the deepth is odd, such as 3,5,7 my code will crash at this line ( I have test it in local ), I do not know why this happen
TreeLinkNode** nowFloor = new TreeLinkNode*[nowNum];
//put nodes of current layer into nowFloor
for(int i = 0, j = 0; i < upNum; i++, j += 2)
{
nowFloor[j] = upFloor[i]->left;
nowFloor[j + 1] = upFloor[i]->right;
}
//connecting
for(int k = 0; k < nowNum - 1; k++)
{
nowFloor[k]->next = nowFloor[k + 1];
}
nowFloor[nowNum - 1]->next = NULL;
//release the memory of upper layer
delete[] upFloor;
//recurrence
deep++;
if(deep <= maxDeep)
{
doConnect(nowFloor, deep, maxDeep, lengthOfEachFloor);
}
else
{
delete[] nowFloor;
}
}
//return the counts of each layer
int* ElementCounter(int deep)
{
int len = deep + 1;
int* result = new int[len];
result[0] = 0;
result[1] = 1;
for(int i = 2; i < len + 1; i++)
{
result[i] = 2 * result[i - 1];
}
return result;
}
//get deepth of the tree
int deepCounter(TreeLinkNode *root)
{
int deep = 0;
while(root != NULL)
{
deep++;
root = root->left;
}
return deep;
}
};
```