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

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define MaxSize 105
//change char to int
typedef int ElemType;
struct BTNode
{
    ElemType data;
    struct BTNode *lchild;
    struct BTNode *rchild;
};
int path[MaxSize]={0},top=0;
int fun(char *str,int start,int *end)
{
    int i,ans=0;
    for(i=start;str[i]!='\0'&&str[i]!=','&&str[i]!='('&&str[i]!=')';i++)
    {
        ans=ans*10+(str[i]-'0');
    }
    *end=i;
    return ans;
}
void CreateBTNode(BTNode *&b,char *str)
{
    BTNode *St[MaxSize],*p=NULL;
    int top=-1,k,j=0,end;
    char ch;
    b=NULL;
    ch=str[j];
    while(ch!='\0')
    {
        switch(ch)
        {
            case '(':
                top++;
                St[top]=p;
                k=1;
                break;
            case ')':
                top--;
                break;
            case ',':
                k=2;
                break;
            default:
                p=(BTNode *)malloc(sizeof(BTNode));
                p->data=fun(str,j,&end);
                j=end-1;
                p->lchild=p->rchild=NULL;
                if(b==NULL)
                    b=p;
                else
                {
                    switch(k)
                    {
                        case 1:
                            St[top]->lchild=p;
                            break;
                        case 2:
                            St[top]->rchild=p;
                            break;
                    }
                }
        }
        j++;
        ch=str[j];
    }
}
void InOrder(BTNode *root)
{
    if(root!=NULL)
    {
        InOrder(root->lchild);
        printf("%d ",root->data);
        InOrder(root->rchild);
    }
}
void DispBTNode(BTNode *b)
{
    if(b!=NULL)
    {
        printf("%d ",b->data);
        if(b->lchild!=NULL||b->rchild!=NULL)
        {
            printf("(");
            DispBTNode(b->lchild);
            if(b->rchild!=NULL)
                printf(",");
            DispBTNode(b->rchild);
            printf(")");
        }
    }
}
void printPath()
{
    int i;
    for(i=0;i<top;i++)
        printf("%d ",path[i]);
    printf("\n");
}
void helper(BTNode *root,int sum)
{
    if(root==NULL)
        return ;
    path[top++]=root->data;
    sum-=root->data;
    if(root->lchild==NULL&&root->rchild==NULL)
    {
        if(sum==0)
        {
            cout<<"hello"<<endl;
            printPath();
        }
    }
    else
    {
        if(root->lchild!=NULL)
            helper(root->lchild,sum);
        if(root->rchild!=NULL)
            helper(root->rchild,sum);
    }
    top--;
    sum+=root->data;
}
int main(int argc,char *argv[])
{
    freopen("input.txt","r",stdin);
    char ch[MaxSize]={'\0'};
    int num,i,sum;
    cin.getline(ch,80);
    BTNode *root;
    printf("ch=%s\n",ch);
    CreateBTNode(root,ch);
    InOrder(root);
    printf("\n");
    DispBTNode(root);
    printf("\n");
    scanf("%d",&sum);
    helper(root,sum);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值