hanks
BAN USERBecause for a tree with 0 or 2 children, number of leaf = Number of Non-leaf + 1.
we can separate left and right tree with this fact.
I have the following code
Node *BuildTree(char A[ ], int size)
{
if (size == 1){
if (A[0] == 'L') {
Node *root = new Node(A[0]);
root ->left = NULL;
root->right = NULL;
return root;
}else
return NULL;
}
Node *root = new Node(A[0]);
root ->left = NULL;
root->right = NULL;
int N_count = 0;
int L_count = 0;
int i = 1;
while (L_count <=N_count){
if(A[i] == 'N') N_number ++;
else L_Number ++;
i++;
}
root ->left = BuildTree(A+1, i-1);
root->right = BuildTree(A+i+1, size - i- 1);
return root;
}
1st Question: Use a size-K queue
- hanks February 09, 20122nd Question: Use 10 queues