Code is not complete…. So. Don’t read it.
Code is not optimized. It is just to show the working.
#include <stdio.h> #include <stdlib.h> typedef struct nodes{ int val; struct nodes *left; struct nodes *right; }node; void dfs(node **,int); int main() { node *one = (node *)malloc(sizeof(node)); node *two = (node *)malloc(sizeof(node)); node *three = (node *)malloc(sizeof(node)); node *four = (node *)malloc(sizeof(node)); node *five = (node *)malloc(sizeof(node)); one->val = 1; one->left = two; one->right = three; two->val = 2; two->left = four; two->right = five; three->val = 3; three->left = four; three->right = five; four->val = 4; four->left = NULL; four->right = NULL; five->val = 5; five->left = NULL; five->right = NULL; node **queuee = (node **)malloc( 100 * sizeof(node*)); queuee[0] = one; int left = 0; int right = 0; while(left<=right){ node *temp = queuee[left]; left ++; printf("%d",temp->val); if(temp->left!=NULL) { right++; queuee[right] = temp->left; } if(temp->right!=NULL) { right++; queuee[right] = temp->right; } } return 0; }