예제 #1
0
int printPathsRecur(struct node* node, int path[], int pathLen) 
{
  if (node==NULL) 
    return 0;
 
  path[pathLen] = node->data;
  pathLen++;
 
  int laser = 0;  

  if (node->left==NULL && node->right==NULL) 
  {
    for(i=0;i<pathLen;i++)
        laser=laser*10 + path[i];
    sum+=laser;
  }
  else
  {
   
    p = printPathsRecur(node->left, path, pathLen);
    q = printPathsRecur(node->right, path, pathLen);
  }
  
  return sum;
}
void printPathsRecur(struct node* n, int *path,int pathLen) {

if(n != NULL) {
  path[pathLen++] = n->data;
  if(n->left != NULL || n->right != NULL) {
     if(n->right == NULL) {
       printPathsRecur(n->left,path,pathLen);
     } 
     else if(n->left == NULL) {
       printPathsRecur(n->right,path,pathLen);
     }
     else {
       printPathsRecur(n->left,path,pathLen);
       printPathsRecur(n->right,path,pathLen);

     } 

  }
  else {
    for(int i=0;i<pathLen;i++)
      printf("%d ",path[i]);
       
    printf("\n-----------------------\n");
  }
}

}
예제 #3
0
파일: BST.c 프로젝트: richzw/CodeHome
void printPathsRecur(struct node* node, int path[], int pathLen){
   if (NULL == node)
       return ;
   path[pathLen] = node->data;
   pathLen++;
   if (NULL == node->left && NULL == node->right){
       for (int index = 0; index < pathLen; ++pathLen)
           cout << path[index] << " ";
       cout << endl;
   }else{
       printPathsRecur(node->left, path, pathLen);
       printPathsRecur(node->right, path, pathLen);
   }
}
/* 
 Recursive helper function -- given a node, and an array containing 
 the path from the root node up to but not including this node, 
 print out all the root-leaf paths. 
*/ 
void printPathsRecur(struct node* node, int path[], int pathLen)
{
   if(node ==NULL  )
   {    return ;   }
	
   path[pathLen]=node->data;
	pathLen++;
   if (node ->left==NULL && node ->right==NULL) 
   {    printArray( path ,pathLen);   }
   else 
   {
   printPathsRecur (node ->left,path,pathLen);
   printPathsRecur(node->right,path,pathLen);
   }
}
예제 #5
0
/* Recursive helper function -- given a node, and an array containing
 the path from the root node up to but not including this node,
 print out all the root-leaf paths. */
void printPathsRecur(struct node* root, int path[], int pathLen) 
{
  if (root==NULL)
  	return;
  path[pathLen++] = root->data;
  
  if(root->left == NULL && root->right == NULL)
  	printArray( path, pathLen);
  
  else {
  		printPathsRecur(root->right, path, pathLen);
  		printPathsRecur(root->left, path, pathLen);
  }
  
}
예제 #6
0
void printPathsRecur(struct node* node, int path[], int pathlen)
{
	if (node==NULL)
		return;
	
	path[pathlen]=node->data;
	pathlen++;

	if(node->left==NULL && node->right==NULL)
		printArray(path,pathlen);
	else
	{
		printPathsRecur(node->left,path,pathlen); //PREORDER!!!!!!
		printPathsRecur(node->right,path,pathlen);
	}
}
예제 #7
0
/* Recursive helper function -- given a node, and an array containing
 the path from the root node up to but not including this node,
 print out all the root-leaf paths.*/
void printPathsRecur(treeNode* node, char *path[], int pathLen) {
	if (node == NULL)
		return;

	/* append this node to the path array */
	path[pathLen] = (char *) node->key;
	pathLen++;

	/* it's a leaf, so print the path that led to here  */
	if (node->left == NULL && node->right == NULL) {
		printArray(path, pathLen);
	} else {
		/* otherwise try both subtrees */
		printPathsRecur(node->left, path, pathLen);
		printPathsRecur(node->right, path, pathLen);
	}
}
예제 #8
0
// Given a binary tree, print out all of its root-to-leaf paths as defined above
// use this by calling printPaths(struct node* node)
void printPathsRecur(struct node* node, int pathLen) {
    int i;
    static int path[1000];

    if (!node) return;

    path[pathLen] = node->data;
    pathLen++;

    if (!node->left && !node->right) {
        for (i = 0; i < pathLen; i++)
            printf("%d ", path[i]);
        printf("\n");
    }

    printPathsRecur(node->left, pathLen);
    printPathsRecur(node->right, pathLen);
}
예제 #9
0
/* Recursive helper function -- given a node, and an array containing
 the path from the root node up to but not including this node,
 print out all the root-leaf paths.*/
void printPathsRecur(struct node* node, int path[], int pathLen, int *sum)
{
  if (node==NULL)
    return;
 
  /* append this node to the path array */
  path[pathLen] = node->data;
  pathLen++;
 
  /* it's a leaf, so print the path that led to here  */
  if (node->left==NULL && node->right==NULL)
  {
    printArray(path, pathLen,sum);
  }
  else
  {
    /* otherwise try both subtrees */
    printPathsRecur(node->left, path, pathLen,sum);
    printPathsRecur(node->right, path, pathLen,sum);
  }
}
예제 #10
0
void printPaths(struct node* node)
{
	int path[1000];
	printPathsRecur(node,path,0);
}
/* 
 Given a binary tree, print out all of its root-to-leaf 
 paths, one per line. Uses a recursive helper to do the work. 
*/ 
void printPaths(NODE * node) { 
  int path[1000]; 
  printPathsRecur(node, path, 0); 
} 
예제 #12
0
/*Given a binary tree, print out all of its root-to-leaf
 paths, one per line. Uses a recursive helper to do the work.*/
void printPaths(treeNode *node) {
	char *path[1000];
	printPathsRecur(node, path, 0);
}
예제 #13
0
void printPaths(struct node* node) {
    printPathsRecur(node, 0);
}
void printPaths(struct node* n) {
int *path = (int *)malloc(sizeof(int) * maxDepth(n));
printPathsRecur(n,path,0);

}
예제 #15
0
/*Given a binary tree, print out all of its root-to-leaf
 paths, one per line. Uses a recursive helper to do the work.*/
void printPaths(struct node* node, int *sum)
{
  int path[1000];
  printPathsRecur(node, path, 0,sum);
}