C function to print all paths in binary tree from root to leaf.
For a given binary tree o/p should be :
Print all paths o/p :
8 3 1
8 3 6 4
8 3 6 7
8 10 14 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
void printpaths(struct Tnode *root){ int path[800]; printpathrec(root,path,0); } void printarray(int path[],int pathlen){ int i; for(i=0;i<pathlen;i++) printf(" %d ",path[i]); printf("\n"); } void printpathrec(struct Tnode *root, int path[],int pathlen){ if(root==NULL) return; path[pathlen]=root->val; pathlen++; if(root->left==NULL && root->right==NULL){ printarray(path,pathlen); } else { printpathrec(root->left,path,pathlen); printpathrec(root->right,path,pathlen); } } |
==> Click Here to Get Complete C Code of Binary Search Tree implementation
==> Click here to Get Code for all Path Print and Binary Tree implementation
References : http://cslibrary.stanford.edu/110/BinaryTrees.html