Algorithm to print all paths with a given sum in a binary tree

后端 未结 18 1395
既然无缘
既然无缘 2020-12-24 07:04

The following is an interview question.

You are given a binary tree (not necessarily BST) in which each node contains a value. Design an algorithm t

18条回答
  •  星月不相逢
    2020-12-24 07:20

    # include
    # include 
    struct Node
    {
        int data;
        struct Node *left, *right;
    };
    
    struct Node * newNode(int item)
    {
        struct Node *temp =  (struct Node *)malloc(sizeof(struct Node));
        temp->data = item;
        temp->left =  NULL;
        temp->right = NULL;
        return temp;
    }
    void print(int p[], int level, int t){
        int i;
        for(i=t;i<=level;i++){
            printf("\n%d",p[i]);
        }
    }
    void check_paths_with_given_sum(struct Node * root, int da, int path[100], int level){
    
         if(root == NULL)
            return ;
        path[level]=root->data;
        int i;int temp=0;
        for(i=level;i>=0;i--){
            temp=temp+path[i];
            if(temp==da){
                print(path,level,i);
            }
        }
            check_paths_with_given_sum(root->left, da, path,level+1);
            check_paths_with_given_sum(root->right, da, path,level+1);
    
    }
    int main(){
        int par[100];
     struct Node *root = newNode(10);
        root->left = newNode(2);
        root->right = newNode(4);
        root->left->left = newNode(1);
        root->right->right = newNode(5);
        check_paths_with_given_sum(root, 9, par,0);
    
    
    }
    

    This works.....

提交回复
热议问题