SCHOOL OF CODE BUILDERS
Learn To CODE. Become A DEVELOPER.
Pages
HOME
DATA STRUCTURES
STRINGS
ARRAYS
MATRIX
BINARY TREES
LINKED LIST
STACK
QUEUE
SORTING
SEARCHING
C
PYTHON
PSEUDOCODE
CONTEST PROBLEMS
ALGORITHMS
PATTERNS
PHP
C PUZZLES
C INTERVIEW QUESTIONS
JAVA
C++
HASHING
RECURSION
BASIC C PROGRAMS
TCS-CODEVITA
FACEBOOK
CONTACT US
Level order traversal of a Binary Tree
Level order traversal of a Binary Tree
#include
#include
struct node { int data; struct node *left,*right; }; struct node* insert(int data) { struct node *temp=(struct node*)malloc(sizeof(struct node)); temp->data=data; temp->left=temp->right=NULL; return temp; } void inorder(struct node *root) { if(root==NULL) return; inorder(root->left); printf("%d ",root->data); inorder(root->right); } int findmax(int x,int y) { return x>y?x:y; } int finddepth(struct node *root) { if(root==NULL) return 0; int ldepth=finddepth(root->left); int rdepth=finddepth(root->right); return 1 + findmax(ldepth,rdepth); } void printlevel(struct node *root,int level) { if(root==NULL) return; if(level==0) printf("%d ",root->data); else { printlevel(root->left,level-1); printlevel(root->right,level-1); } } void levelordertraversal(struct node *root) { int depth=finddepth(root); //printf("depth of the tree=%d",depth); int i; for(i=0;i
left=insert(2); root->left->left=insert(4); root->left->right=insert(5); root->left->left->left=insert(8); root->left->left->right=insert(9); root->left->right->left=insert(10); root->left->right->right=insert(11); root->right=insert(3); root->right->left=insert(6); root->right->right=insert(7); root->right->left->left=insert(12); root->right->left->right=insert(13); root->right->right->left=insert(14); root->right->right->right=insert(15); //inorder(root); levelordertraversal(root); return 0; }
PREVIOUS
NEXT
HOME