在二叉树中找出和为某值的所有路径

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 /*在二叉树中找出和为某值的所有路径*/
  4 /*从树的根节点开始往下访问一直到叶节点所经过的所有节点形成一条路径*/
  5 /*分析,二叉树肯定要递归,遍历每条路径,得到每条路径和。
  6  *路径的存储用栈结构比较合适,链表,数组都可。
  7  */
  8 typedef struct node{
  9     int value;
 10     struct node *left;
 11     struct node *right;
 12 }node;
 13 
 14 static int total;
 15 #define STACK_SIZE 100
 16 static int stack[STACK_SIZE], top_element = -1;
 17 
 18 int is_empty()
 19 {
 20     return top_element == -1;
 21 }
 22 int is_full()
 23 {
 24     return top_element == STACK_SIZE-1;
 25 }
 26 void push(int value)
 27 {
 28     if(is_full()){
 29         printf("stack is full.\n");
 30         return;
 31     }
 32     top_element++;
 33     stack[top_element] = value;
 34 }
 35 void pop()
 36 {
 37     if(is_empty()){
 38         printf("stack is empty.\n");
 39         return;
 40     }
 41     top_element--;
 42 }
 43 int top()
 44 {
 45     if(is_empty()){
 46         printf("stack is empty.\n");
 47         exit(0);
 48     }
 49     return stack[top_element];
 50 }
 51 
 52 void top_all()
 53 {
 54     if(is_empty()){
 55         printf("stack is empty.\n");
 56         exit(0);
 57     }
 58     int i;
 59 
 60     for(i = 0; i <= top_element; i++)
 61         printf("%d ", stack[i]);
 62     printf("\n");
 63 }
 64 
 65 void build_tree(node **tree, int value)
 66 {
 67     if(*tree == NULL){
 68         (*tree) = calloc(1, sizeof(node));
 69         if(*tree == NULL){
 70             perror("malloc failure:");
 71             return;
 72         }
 73         (*tree)->value = value;
 74     }else if((*tree)->value > value)
 75         build_tree(&(*tree)->left, value);
 76     else if((*tree)->value == value)
 77         ;
 78     else
 79         build_tree(&(*tree)->right, value);
 80 }
 81 void read_tree(node *tree)
 82 {
 83     if(tree == NULL)
 84         return;
 85     if(tree->left != NULL)
 86         read_tree(tree->left);
 87     printf("%d\n", tree->value);
 88     if(tree->right != NULL)
 89         read_tree(tree->right);
 90 }
 91 /*路径,需前序遍历,先根节点,左右。
 92  *理解遍历过程,适时减去加上节点值。
 93  */
 94 void read_tree2(node *tree, int size)
 95 {
 96     if(tree == NULL)
 97         return;
 98 
 99     total += tree->value;
100     push(tree->value);
101     if(tree->left == NULL && tree->right == NULL)
102         if(total == size)
103             top_all();
104 
105     if(tree->left != NULL)
106         read_tree2(tree->left, size);
107 
108     if(tree->right != NULL)
109         read_tree2(tree->right, size);
110 
111     total -= tree->value;
112     pop(tree->value);
113 }
114 int main(void)
115 {
116     node *tree = NULL;
117 
118     build_tree(&tree, 10);
119     build_tree(&tree, 12);
120     build_tree(&tree, 5);
121     build_tree(&tree, 4);
122     build_tree(&tree, 7);
123 
124     read_tree2(tree, 22);
125 
126     return 0;
127 }

 

转载于:https://www.cnblogs.com/kevin-kang/p/3181250.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值