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
Print the paths whose sum of values equals given sum
Print the paths whose sum of values equals given sum
#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 calsum(int *path,int n) { int sum=0; for(--n;n>=0;n--) sum += *(path+n); return sum; } void printarray(int *path,int n) { int i; for(i=0;i
data; pathlen++; if(calsum(path,pathlen)==sum) { printarray(path,pathlen); printf("\n"); } else { printpathsum(root->left,sum,path,pathlen); printpathsum(root->right,sum,path,pathlen); } } int main(void) { struct node *root=NULL; int *path,pathlen; path=(int*)malloc(sizeof(int)*1000); root=insert(1); root->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); printpathsum(root,11,path,pathlen); return 0; }
PREVIOUS
NEXT
HOME