Convert recursive implementation to iterative implementation using stack - c

I would like to write function which will count the number of possible solutions there are for a given Sudoku board.
In addition, I would like it to be implemented as an iterative solution using a stack.
I found this recursive solution, which is implemented in Java, and I've written it in C (it is first called with countSolutionsRec(board, 0, 0, 0), where board is 2D array of integers which represents the Sudoku board game):
int countSolutionsRec(int board[9][9], int i, int j, int counter) {
int value;
if (i == 9) {
i = 0;
if (++j == 9){
return 1+counter;
}
}
if (board[i][j] != 0){
return countSolutionsRec(board, i+1, j, counter);
}
for (value = 1; value <= 9; ++value) {
if (validCheck(board, j, i, value)){ //This function checks if the value is a legal value to place in board[i][j] according to sudoku rules
board[i][j]= value;
counter = countSolutionsRec(board, i+1, j, counter);
}
}
board[i][j] = 0;
return counter;
}
Then, I've tried to follow this guide to convert the above code to an iterative implementation using a stack, and this is what I came up with:
int countSolutions(int board[9][9]) {
int returnValue, value;
SnapShotStruct newSnapshot;
StackNode* snapshotStack;
SnapShotStruct currentSnapshot;
currentSnapshot.i = 0;
currentSnapshot.j = 0;
currentSnapshot.counter = 0;
push(&snapshotStack, currentSnapshot);
while (!empty(snapshotStack)) {
currentSnapshot=top(snapshotStack);
pop(&snapshotStack);
if (currentSnapshot.i == 9){
currentSnapshot.i = 0;
if (++currentSnapshot.j == 9) {
returnValue = currentSnapshot.counter + 1;
continue;
}
}
if (board[currentSnapshot.i][currentSnapshot.j] != 0) {
newSnapshot.i = currentSnapshot.i + 1;
newSnapshot.j = currentSnapshot.j;
newSnapshot.counter = currentSnapshot.counter;
push(&snapshotStack, newSnapshot);
continue;
}
for (value = 1; value <= 9; ++value) {
if (validCheck(board, currentSnapshot.j, currentSnapshot.i, value)){
board[currentSnapshot.i][currentSnapshot.j] = value;
newSnapshot.i = currentSnapshot.i + 1;
newSnapshot.j = currentSnapshot.j;
newSnapshot.counter = returnValue;
push(&snapshotStack, newSnapshot);
continue;
}
}
board[currentSnapshot.i][currentSnapshot.j] = 0;
}
return returnValue;
}
My stack implementation:
typedef struct SnapShotStruct {
int i;
int j;
int counter;
int stage;
} SnapShotStruct;
typedef struct StackNode {
struct SnapShotStruct snapshot;
struct StackNode* next;
} StackNode;
StackNode* newNode(SnapShotStruct snapshot) {
StackNode* stackNode = (StackNode*) malloc(sizeof(StackNode));
stackNode->snapshot.i = snapshot.i;
stackNode->snapshot.j = snapshot.j;
stackNode->snapshot.counter = snapshot.counter;
stackNode->snapshot.stage = snapshot.stage;
stackNode->next = NULL;
return stackNode;
}
int empty(StackNode *root) {
return !root;
}
void push(StackNode** root, SnapShotStruct snapshot) {
StackNode* stackNode = newNode(snapshot);
stackNode->next = *root;
*root = stackNode;
}
SnapShotStruct pop(StackNode** root) {
SnapShotStruct popped;
StackNode* temp = *root;
if (empty(*root))
return popped;
*root = (*root)->next;
popped = temp->snapshot;
free(temp);
return popped;
}
SnapShotStruct top(StackNode* root) {
SnapShotStruct snapshot;
if (empty(root))
return snapshot;
snapshot = root->snapshot;
return snapshot;
}
The problem is that I get wrong results from the iterative implementation.
I think the issue is the way I've converted the following line:
counter = countSolutionsRec(board, i+1, j, counter);
But I'm not sure how to fix it and get my iterative implementation to return the correct results.
Any help is appreciated.

Related

Binary searching in tree

I need to create this tree:
the tree
I need to implement a function that takes the tree's node (root), a number, and returns how many times the binary representation of the number occurs in the tree.
For example:
9(1001) occurs 4 times
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct TreeNode {
int val;
struct TreeNode* leftNode;
struct TreeNode* rightNode;
};
void strrev(char * arr, int start, int end) {
char temp;
if (start >= end) {
return;
}
temp = * (arr + start);
*(arr + start) = * (arr + end);
*(arr + end) = temp;
start++;
end--;
strrev(arr, start, end);
}
char * itoa(int number, char * arr, int base) {
int i = 0, r, negative = 0;
if (number == 0) {
arr[i] = '0';
arr[i + 1] = '\0';
return arr;
}
if (number < 0 && base == 10) {
number *= -1;
negative = 1;
}
while (number != 0) {
r = number % base;
arr[i] = (r > 9) ? (r - 10) + 'a' : r + '0';
i++;
number /= base;
}
if (negative) {
arr[i] = '-';
i++;
}
strrev(arr, 0, i - 1);
arr[i] = '\0';
return arr;
}
int count_occurrences(struct TreeNode* root, int number) {
int count = 0;
char binary[33];
itoa(number, binary, 2);
int length = strlen(binary);
struct TreeNode* current = root;
for (int i = 0; i < length; i++) {
if (binary[i] == '0') {
if (current->leftNode != NULL) {
current = current->leftNode;
} else {
return count;
}
} else {
if (current->rightNode != NULL) {
current = current->rightNode;
} else {
return count;
}
}
if (current->val == number) {
count++;
}
}
return count;
}
struct TreeNode* createNode(int value) {
struct TreeNode* newNode = malloc(sizeof(struct TreeNode));
newNode->val = value;
newNode->leftNode = NULL;
newNode->rightNode = NULL;
return newNode;
}
struct TreeNode* insertLeftNode(struct TreeNode* rootNode, int value) {
rootNode->leftNode = createNode(value);
return rootNode->leftNode;
}
struct TreeNode* insertRightNode(struct TreeNode* rootNode, int value) {
rootNode->rightNode = createNode(value);
return rootNode->rightNode;
}
int main() {
struct TreeNode* rootNode = createNode(1);
insertLeftNode(rootNode, 1);
insertRightNode(rootNode, 0);
insertLeftNode(rootNode->leftNode, 0);
insertRightNode(rootNode->leftNode, 1);
insertLeftNode(rootNode->rightNode, 0);
insertRightNode(rootNode->rightNode, 0);
insertLeftNode(rootNode->leftNode->leftNode, 0);
insertRightNode(rootNode->leftNode->leftNode, 0);
insertLeftNode(rootNode->rightNode->rightNode, 0);
insertRightNode(rootNode->rightNode->rightNode, 0);
printf("\n%d", count_occurrences(rootNode, 9));
}
I am using itoa(), but for some reasons it doesn't return nothing.
Itoa gave compiler warning so i copied it from internet.
Also I think i am not correctly creating the tree.

What can I do to make my code generate an output?

#include <stdio.h>
#include <stdlib.h>
typedef enum TypeTag {
ADD,
SUB,
MUL,
DIV,
ABS,
FIB,
} TypeTag;
typedef struct Node {
TypeTag type;
int value;
struct Node *left;
struct Node *right;
} Node;
#define MAXN 100
int fib[MAXN];
// function to create a new node
Node* makeFunc(TypeTag type) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->type = type;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// fibonacci function using dynamic programming
int fibonacci(int n) {
int fib[n+1];
fib[0] = 0;
fib[1] = 1;
for(int i = 2; i <= n; i++) {
fib[i] = fib[i-1] + fib[i-2];
}
return fib[n];
}
// function to calculate the value of a node
int calc(Node* node) {
if (node->type == ADD) {
return calc(node->left) + calc(node->right);
}
else if (node->type == SUB) {
return calc(node->left) - calc(node->right);
}
else if (node->type == MUL) {
return calc(node->left) * calc(node->right);
}
else if (node->type == DIV) {
return calc(node->left) / calc(node->right);
}
else if (node->type == ABS) {
return abs(calc(node->left));
}
else if (node->type == FIB) {
return fibonacci(calc(node->left));
}
return node->value;
}
int main() {
for (int i = 0; i < MAXN; i++) {
fib[i] = -1;
}
Node *add = makeFunc(ADD);
add->left = makeFunc(10);
add->right = makeFunc(6);
Node *mul = makeFunc(MUL);
mul->left = makeFunc(5);
mul->right = makeFunc(4);
Node *sub = makeFunc(SUB);
sub->left = makeFunc(calc(add));
sub->right = makeFunc(calc(sub));
Node *fibo = makeFunc(FIB);
fibo->left = makeFunc(abs(calc(sub)));
fibo->value = fibonacci(calc(fibo->left));
printf("add : %d\n", calc(add));
printf("mul : %d\n", calc(mul));
printf("sub : %d\n", calc(sub));
printf("fibo : %d\n", calc(fibo));
printf("Hello world");
// return 0;
}
I think the problem is coming from the makeFunc function but i am not sure what else to do.I tried to print hello world to see if the problem is from my implementations but it still did not print.
I was trying to solve this problem
{
TypeTag type;
} Node;
typedef enum TypeTag {
...
}
Using this structure, please write a function that returns fibonacci sequence based on the following arithmetic operations (+, -,
*, /) and conditions. The fibonacci function should be implemented using Dynamic Programming.
main()
{
Node *add = (*makeFunc(ADD))(10, 6);
Node *mul = (*makeFunc(MUL))(5, 4);
Node *sub = (*makeFunc(SUB))(mul, add);
Node *fibo = (*makeFunc(SUB))(sub, NULL);
calc(add);
calc(mul);
calc(sub);
calc(fibo)
}
Output
add : 16
mul : 20
sub : -4
fibo : 2
You can't use makeFunc(4) to make the representation of the number 4, because 4 == ABS. You should use a separate type for nodes that represent numbers rather than functions. Then use another function to create nodes of this type.
Another problem is here:
sub->right = makeFunc(calc(sub));
You can't call calc(sub) until after you've filled in both sub->left and sub->right. I'm guessing you meant to use calc(mul).
#include <stdio.h>
#include <stdlib.h>
typedef enum TypeTag {
ADD,
SUB,
MUL,
DIV,
ABS,
FIB,
LIT
} TypeTag;
typedef struct Node {
TypeTag type;
int value;
struct Node *left;
struct Node *right;
} Node;
#define MAXN 100
int fib[MAXN];
// function to create a new node
Node* makeFunc(TypeTag type) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->type = type;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
Node* makeValue(int value) {
Node *newNode = makeFunc(LIT);
newNode->value = value;
return newNode;
}
// fibonacci function using dynamic programming
int fibonacci(int n) {
int fib[n+1];
fib[0] = 0;
fib[1] = 1;
for(int i = 2; i <= n; i++) {
fib[i] = fib[i-1] + fib[i-2];
}
return fib[n];
}
// function to calculate the value of a node
int calc(Node* node) {
if (node->type == ADD) {
return calc(node->left) + calc(node->right);
}
else if (node->type == SUB) {
return calc(node->left) - calc(node->right);
}
else if (node->type == MUL) {
return calc(node->left) * calc(node->right);
}
else if (node->type == DIV) {
return calc(node->left) / calc(node->right);
}
else if (node->type == ABS) {
return abs(calc(node->left));
}
else if (node->type == FIB) {
return fibonacci(calc(node->left));
}
else if (node->type == LIT) {
return node->value;
}
printf("Invalid node type %d\n", node->type);
exit(1);
}
int main() {
for (int i = 0; i < MAXN; i++) {
fib[i] = -1;
}
Node *add = makeFunc(ADD);
add->left = makeValue(10);
add->right = makeValue(6);
Node *mul = makeFunc(MUL);
mul->left = makeValue(5);
mul->right = makeValue(4);
Node *sub = makeFunc(SUB);
sub->left = makeValue(calc(add));
sub->right = makeValue(calc(mul));
Node *fibo = makeFunc(FIB);
fibo->left = makeValue(abs(calc(sub)));
fibo->value = fibonacci(calc(fibo->left));
printf("add : %d\n", calc(add));
printf("mul : %d\n", calc(mul));
printf("sub : %d\n", calc(sub));
printf("fibo : %d\n", calc(fibo));
printf("Hello world\n");
// return 0;
}

Why is my Hash Map insert function not working?

I am making a Hash Map with people's names as its key using C language. I am using separate chaining to resolve the collision.
This is my code:
#include<stdio.h>
#include<stdlib.h>
#define MinTableSize 1
#include <stdbool.h>
//Colission resolution Using linked list
struct ListNode;
typedef struct ListNode *Position;
struct HashTbl;
typedef struct HashTbl *HashTable;
typedef unsigned int Index;
Index Hash(const char *Key, int Tablesize)
{
unsigned int HashVal = 0;
while(*Key != '\0')
{
HashVal += *Key++;
}
return HashVal % Tablesize;
}
struct ListNode
{
int Element;
Position Next;
};
typedef Position List;
struct HashTbl
{
int TableSize;
List *TheLists;
};
//Function to find next prime number for the size
bool isPrime(int n)
{
if(n <= 1)
{
return false;
}
if(n <= 3)
{
return true;
}
if(n%2 == 0 || n%3 == 0)
{
return false;
}
for(int i = 5; i*i <= n; i = i + 6)
{
if(n%i == 0 || n%(i + 2) == 0)
{
return false;
}
}
return true;
}
int NextPrime(int N)
{
if(N <= 1)
{
return 2;
}
int prime = N;
bool found = false;
while(!found)
{
prime++;
if(isPrime(prime))
{
found = true;
}
}
return prime;
}
HashTable InitializeTable(int TableSize)
{
HashTable H;
int i;
if(TableSize < MinTableSize)
{
printf("Table size is too small\n");
return NULL;
}
H = malloc(sizeof(struct HashTbl));
if(H == NULL)
{
printf("Out of space\n");
return NULL;
}
H->TableSize = NextPrime(TableSize);
H->TheLists = malloc(sizeof(List) * H->TableSize);
if(H->TheLists == NULL)
{
printf("Out of space\n");
return NULL;
}
for(i = 0; i < H->TableSize; i++)
{
H->TheLists[i] = malloc(sizeof(struct ListNode));
if(H->TheLists[i] == NULL)
{
printf("Out of space\n");
return NULL;
}
else
{
H->TheLists[i]->Next = NULL;
}
}
return H;
}
//funtion to find the value
Position Find(const char *Key, HashTable H)
{
Position P;
List L;
L = H->TheLists[Hash(Key, H->TableSize)];
P = L->Next;
while(P != NULL && P->Element != Key)
{
P = P->Next;
}
return P;
}
void Insert(const char *Key, HashTable H)
{
Position Pos;
Position NewCell;
List L;
Pos = Find(Key, H);
if(Pos == NULL)
{
NewCell = malloc(sizeof(struct ListNode));
if(NewCell == NULL)
{
printf("Out of space\n");
return NULL;
}
else
{
L = H->TheLists[Hash(Key, H->TableSize)];
NewCell->Next;
NewCell->Element = Key;
L->Next = NewCell;
printf("Key inserted\n");
}
}
else
{
printf("Key already exist\n");
}
}
int main()
{
char Name[6][20] = {"Joshua", "Erica", "Elizabeth", "Monica", "Jefferson", "Andrian"};
int Size = sizeof(Name[0])/sizeof(Name[0][0]);
HashTable H = InitializeTable(Size);
Insert(Name[0], H);
Insert(Name[1], H);
Insert(Name[2], H);
Insert(Name[3], H);
}
The putout of this code is:
Key inserted
Key inserted
This means, it only successfully inserted two keys, while the other name has not been inserted. I think there's some error in my Insert() function, but I got no clue. I tried using an online compiler and it compile properly.

priority queue utilizing a heap working except for freeC

I have my priority queue working and printing as I expected it to, but the free is invalid and I'm not sure why, i'm following all the protocols for the heap that I have learned so far, but my program will not finish.
#include <stdlib.h>
#include "priority_queue.h"
struct item{
int priority;
int value;
};
typedef struct item Item;
struct priority_queue{
int size;
int capacity;
int front;
Item* data;
};
typedef struct priority_queue Priority_queue;
void priority_queue_fix_down(PRIORITY_QUEUE hQueue, int index, int size);
PRIORITY_QUEUE priority_queue_init_default(void){
Priority_queue* pQueue_ptr;
pQueue_ptr = (Priority_queue*)malloc(sizeof(Priority_queue));
if(pQueue_ptr != NULL){
pQueue_ptr->size = 0;
pQueue_ptr->capacity = 1;
pQueue_ptr->data = (Item*)malloc(sizeof(Item)*pQueue_ptr->capacity);
if (pQueue_ptr->data == NULL){
free(pQueue_ptr);
pQueue_ptr = NULL;
}
}
return pQueue_ptr;
}
Status priority_queue_insert(PRIORITY_QUEUE hQueue, int priority_level, int data_item)
{
Priority_queue* pQueue_ptr = (Priority_queue*)hQueue;
Item* temp, temp2;
//temp = (Item*)malloc(sizeof());
int i;
if (pQueue_ptr->size >= pQueue_ptr->capacity)
{
//not enough space
temp = (Item*)malloc(sizeof(Item) * 2 * pQueue_ptr->capacity);
if (temp == NULL)
{
return FAILURE;
}
for (i = 0; i < pQueue_ptr->size; i++)
{
temp[i] = pQueue_ptr->data[i];
}
pQueue_ptr->front = 0;
pQueue_ptr->capacity *= 2;
free(pQueue_ptr->data);
pQueue_ptr->data = temp;
}
i = pQueue_ptr->size;
(pQueue_ptr->data[i]).priority = priority_level;
(pQueue_ptr->data[i]).value = data_item;
int index_of_parent;
index_of_parent = (i - 1) / 2;
while (i >= 1 && ((pQueue_ptr->data[i]).priority > (pQueue_ptr->data[index_of_parent]).priority))
{
temp2 = pQueue_ptr->data[index_of_parent];
pQueue_ptr->data[index_of_parent] = pQueue_ptr->data[i];
pQueue_ptr->data[i] = temp2;
i = index_of_parent;
index_of_parent = (i - 1) / 2;
}
pQueue_ptr->size++;
// pQueue_ptr->front = 0;
// pQueue_ptr->back = pQueue_ptr->size-1;
return SUCCESS;
}
void print_heap(PRIORITY_QUEUE hQueue){
Priority_queue* pQueue_ptr = (Priority_queue*) hQueue;
for(int x = 0; x<pQueue_ptr->size;x++){
printf("%d\n", pQueue_ptr->data[x].priority);
}
}
Status priority_queue_service(PRIORITY_QUEUE hQueue){
//set variables
Priority_queue* pQueue_ptr = (Priority_queue*) hQueue;
Item temp;
int size, index, index_left_child, index_right_child;
if(pQueue_ptr->size == 0){
return FAILURE;
}
index = 0;
temp = pQueue_ptr->data[0];
pQueue_ptr->data[0] = pQueue_ptr->data[pQueue_ptr->size-1];
pQueue_ptr->data[size-1] = temp;
pQueue_ptr->size--;
priority_queue_fix_down(pQueue_ptr, pQueue_ptr->front, pQueue_ptr->size);
return SUCCESS;
}
int priority_queue_front(PRIORITY_QUEUE hQueue, Status* status)
{
Priority_queue* pPriority_queue = (Priority_queue*)hQueue;
if (priority_queue_is_empty(pPriority_queue))
{
if (status != NULL)
{
*status = FAILURE;
}
return 0;
}
if (status != NULL)
{
*status = SUCCESS;
}
return (pPriority_queue->data[pPriority_queue->front]).value;
}
Boolean priority_queue_is_empty(PRIORITY_QUEUE hQueue){
Priority_queue* pQueue_ptr = (Priority_queue*) hQueue;
if (pQueue_ptr->size <= 0)
return TRUE;
else
return FALSE;
}
This is where I get the error when debugging, on free(pPriority_queue->data); It doesn't even reach the line below. I tried taking someone elses "service" function and it worked, but they implemented "fix down" in their service function, while I'm trying to do it outside in a separate function.
void priority_queue_destroy(PRIORITY_QUEUE* phQueue){
Priority_queue* pPriority_queue = (Priority_queue*)*phQueue;
free(pPriority_queue->data);
free(*phQueue);
// *phQueue = NULL;
return;
}
void priority_queue_fix_down(PRIORITY_QUEUE hQueue, int index, int size){
Priority_queue* pQueue_ptr = (Priority_queue*) hQueue;
Item* temp;
temp = (Item*)malloc(sizeof(Item));
if (temp == NULL)
return NULL;
//int front = priority_queue_front(pQueue_ptr, NULL);
int index_left_child = 2* index + 1;
int index_right_child = 2* index + 2;
// print_heap(pQueue_ptr);
// printf("\nsize: %d\nindex: %d\n", size, index);
// // printf("Front: %d\n", pQueue_ptr->data[0]);
// if(pQueue_ptr->data[index_left_child].priority > (pQueue_ptr->data[index]).priority){
// printf("Left child index %d\nRight child index: %d\nLeft child has highest priority of: %d\n",index_left_child, index_right_child, pQueue_ptr->data[index_left_child].priority);
// }
// else
// printf("Right child index: %d\nLeft child index %d\nRight child has highest priority of: %d\n",index_right_child, index_left_child, pQueue_ptr->data[index_right_child].priority);
if (index_left_child < size){
if (index_right_child < size && pQueue_ptr->data[index_right_child].priority > (pQueue_ptr->data[index_left_child]).priority){
if(pQueue_ptr->data[index_right_child].priority > (pQueue_ptr->data[index]).priority){
*temp = pQueue_ptr->data[index];
pQueue_ptr->data[index] = pQueue_ptr->data[index_right_child] ;
pQueue_ptr->data[index_right_child] = *temp;
priority_queue_fix_down(pQueue_ptr, index_right_child, size);
}
}
if(pQueue_ptr->data[index_left_child].priority > (pQueue_ptr->data[index]).priority){
*temp = pQueue_ptr->data[index];
pQueue_ptr->data[index] = pQueue_ptr->data[index_left_child];
pQueue_ptr->data[index_left_child] = *temp;
priority_queue_fix_down(pQueue_ptr, index_left_child, size);
}
}
}

create an array with the values of the nodes at a level

I have to create an array that contains the values of nodes in a level passed as parameter. The function createArray has to return an array containing the values of the nodes at the level passed as parameter. My code is:
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data) {
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
int numNodesLevel(struct node* root, int level) {
if (root == NULL) {
return 0;
}
else {
if (level == 0) {
return 1;
}
return numNodesLevel(root->left, level - 1) + numNodesLevel(root->right, level - 1);
}
}
int max(int a, int b) {
if(a > b) {
return a;
}
else {
return b;
}
}
int height(struct node* root) {
if (root == NULL) {
return -1;
}
else {
if (root->left == NULL && root->right == NULL) {
return 0;
}
return max(height(root->left), height(root->right)) + 1;
}
}
int maxNodesLevel(struct node* root) {
if (root == NULL) {
return 0;
}
else {
int max = 0;
int h = height(root);
int i = 0;
for (i = 0; i <= h; i++) {
if (numNodesLevel(root, i) > max) {
max = numNodesLevel(root, i);
}
}
return max;
}
}
void fill(struct node* root, int* A, int level, int* i) {
if (root != NULL) {
if (level == 0) {
A[*i] = root->data;
*i = *i + 1;
}
fill(root->left, A, level - 1, i);
fill(root->right, A, level - 1, i);
}
}
int* createArray(struct node* root, int level) {
if (root != NULL) {
int *A = (int*)calloc(maxNodesLevel(root), sizeof(int));
int i = 0;
fill(root, A, level, &i);
return A;
}
}
int main(){
struct node* root = newNode(12);
root->left = newNode(3);
root->right = newNode(16);
root->left->left = newNode(2);
root->left->right = newNode(2);
root->right->left = newNode(2);
root->right->right = newNode(22);
printf("%d", createArray(root,1));
getchar();
return 0;
}
The result should be 3,16 which are the values of the nodes at level 1, but it gives me different numbers like 22721648. These numbers are different every time I run the code, so I think there must be something wrong in the way I use pointers, but I can't figure out where is the error.
Can somebody help me?
The code is correct, but only mistake here is createArray() returns an array and you are considering it as int.
Add following code instead of printf() and it will work.
int level = 1, *arr;
arr = createArray(root, level);
for(int i = 0; i < 2 * level; i++){
printf("%d ", arr[i]);
}
Hope it will help !!

Resources