程序员算法之找出和为某一值的所有路径

View Code
 1 int FindPath(BITREE T,int key,int &cursum,std::vector<int>& vec)

 2 {

 3     if(!T) return 0;

 4     cursum += T->elem;

 5     vec.push_back(T->elem);

 6     

 7     bool isLeaf = (!T->lchild && !T->rchild);

 8     

 9     if(key == cursum &&  isLeaf)

10     {

11         vector<int>::iterator iter = vec.begin();

12         for(;iter != vec.end();iter++)

13             printf("%d ",*iter);

14         printf("\n");

15     }

16     if(T->lchild) FindPath(T->lchild,key,cursum,vec);

17     if(T->rchild) FindPath(T->rchild,key,cursum,vec);

18     cursum -= T->elem;

19     vec.pop_back();

20     return OK;

21 }

 

你可能感兴趣的:(程序员)