Find and all paths for a value in a two-dollar Tree

Source: Internet
Author: User

Enter an integer and a two-dollar tree. From the root node of the tree, access all the nodes that pass through to the leaf nodes to form a path. Prints out all paths equal to the input integers. For example, enter an integer of $ Two and the following tree
10
 / \
5 12
/ \ / \

4 7 8 9

print out two paths: ten, 1210, 5,7 structbinarytreenode//a Nodein thebinarytree
{
Intm_ Nvalue; valueof node
Binarytreenode *m_pleft;//leftchildof node
Binarytreenode *m_pright;//right childof node
/ span>

Structbinarytreenode//A Nodein Thebinarytree
{
Intm_nvalue; valueof node
Binarytreenode *m_pleft; LEFTCHILDOF node
Binarytreenode *m_pright; Right Childof node

}

#include <stdio.h> #include <stdlib.h> #define MAX typedef struct BITREENODE {int data;      struct Bitreenode *left;  struct Bitreenode *right;    }bitreenode_t;      /* Create two fork tree */bitreenode_t * createbstree (int *data,int pos,int len) {bitreenode_t * TR;      if (Pos>=len) {return NULL;          } else {tr = (bitreenode_t *) malloc (sizeof (bitreenode_t));          Tr->data = Data[pos];          Tr->left = Createbstree (data, 2*pos+1, Len);            Tr->right = Createbstree (data, 2*pos+2, Len);      return TR; }}//middle order traversal binary tree void Inordertraverse (bitreenode_t *root) {if (root!=null) {Inordertraverse (root-&   Gt;left); if (NULL! = Root->left | |        NULL! = root->right) printf ("%d not leaf node\n", root->data); elseprintf ("%d \ n", root->data);      Inordertraverse (Root->right);    }}//print path void Printpath (int path[], int top) {int i = 0; for (i=0; i<top; i++)         printf ("%d", path[i]);  printf ("\ n");      }//Find and for the path of sum, the path array holds the values of the paths, top represents the number of elements in each feasible path, void Findpath (bitreenode_t *root, int sum, int top, int path[]) {      path[top++] = root->data;            Sum-= root->data;              if (Root->left = = NULL && root->right==null) {/* leaf node condition */if (sum = = 0) {        Printpath (path, top); Top--;sum + = root->data;                }} else {/* Non-leaf node condition */if (sum <= 0) {if (sum = = 0)        Printpath (path, top); Top--;sum + = root->data;return;          } if (Root->left! = NULL) Findpath (root->left, sum, top, path);      if (root->right!=null) findpath (root->right, sum, top, path);      }} int main () {int data[]={10, 5, 12, 15, 7, 8, 9};        int len=sizeof (data)/sizeof (int);       bitreenode_t * root = createbstree (data, 0, Len); InordertravErse (root);            printf ("\ n");      int sum=22;      int top=0;  int Path[max] = {0};    printf ("-------------------------------------\ n");      Findpath (root, sum, top, path);  return 0; }/* Print 4 5 not leaf node7 node8 leaf node9-------------------------------------10 5 7 10 12*/



Find and all paths for a value in a two-dollar Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.