• 在二元树中找出和为某一值的所有路径


    (回溯算法)

     1 typedef struct BiTNode
     2 {
     3   int data;
     4   BiTNode * lchild;
     5   BiTNode * rchild;
     6 }BiTNode, * BiTree;
     7 
     8 void Function( BiTree T, int sum )
     9 {
    10   int path[ MAXSIZE ];
    11   FindPath( T, sum, path, 0 );
    12 }
    13 
    14 void FindPath( BiTree P, int sum, int path[], int top )
    15 {
    16   if( p )
    17   {
    18     sum -= P->data;  //sum按值传递
    19     if( sum == 0 )
    20     {
    21       path[ top++ ] = P->data;
    22       print( path, top );   //打印path数组top前的元素。top也是按值传递
    23     }
    24     else if( sum > 0 ) 
    25     {
    26       path[ top++ ] = p->data;
    27       FindPath( P->lchild, sum,  path, top );
    28       FindPath( P->rchild, sum,  path, top );
    29     }
    30   }
    31 }
  • 相关阅读:
    响应式开发
    web作业小结
    js的简单数据类型和复杂数据类型
    JavaScript 字符串对象
    JavaScript 数组篇
    JavaScript 对象篇
    spfa优化
    HZNU Training 28 for Zhejiang Provincial Competition 2020
    [kuangbin带你飞]专题十一 网络流
    HZNU Training 26 for Zhejiang Provincial Competition 2020
  • 原文地址:https://www.cnblogs.com/kevinGaoblog/p/2432099.html
Copyright © 2020-2023  润新知