#include <stdio.h>
      #include <stdlib.h>
      #include<string.h>
      struct Node
      {
      	int data;
      	struct Node *left;
      	struct Node *right;
      	
      	Node(int x){
      	    data = x;
      	    left = NULL;
      	    right = NULL;
      	}
      };
      Node *insert(Node *r,int num);
      int sumOfLeafNodes(Node *r);
      void preOrderDisplay(Node *r);
      void inOrderDisplay(Node *r);
      void postOrderDisplay(Node *r);
      int search(Node *r, int num);
      int main(void) {
          int t,n,data;
          scanf("%d",&t);
          while(t--){
              Node *root=NULL;
              scanf("%d",&n);
              //printf("n=%d\n",n);
              while(n--){
                  scanf("%d",&data);
                  //printf("data=%d\n",data);
                  root=insert(root,data);
              }
              //inOrderDisplay(root);
              printf("%d\n",sumOfLeafNodes(root));
          }
      	return 0;
      }
      Node *insert(Node *r,int num){
          if(r==NULL){
              r = new Node(num);
          }else{
              if(num<r->data){
                  r->left=insert(r->left,num);
              }else{
                  r->right=insert(r->right,num);
              }
          }
          return r;
      }
      void preOrderDisplay(Node *r){
          if(r!=NULL){
              printf("%d ",r->data);
              preOrderDisplay(r->left);
              preOrderDisplay(r->right);
          }
      }
      void inOrderDisplay(Node *r){
          if(r!=NULL){
              inOrderDisplay(r->left);
              printf("%d ",r->data);
              inOrderDisplay(r->right);
          }
      }
      void postOrderDisplay(Node *r){
          if(r!=NULL){
              postOrderDisplay(r->left);
              postOrderDisplay(r->right);
              printf("%d ",r->data);
          }
      }
      int search(Node *r,int num){
          if(r==NULL)return 0;
          else if(r->data==num)return 0;
          else if(r->data > num)
              search(r->left,num);
          else 
              search(r->right,num);
      }// } Driver Code Ends
      /* The structure of Node
      struct Node{
          int data;
          Node *left,*right;
      }; */
      /*You are required to complete below method */
      void findSum(Node *r,int *sum){
          if(r==NULL)
              return;
          findSum(r->left,sum);
          findSum(r->right,sum);
          if(r->left==NULL and r->right==NULL)
              *sum=*sum+r->data;
      }
      int sumOfLeafNodes(Node *r ){
          int sum=0;
          findSum(r,&sum);
          return sum;
      }
      
      Cpp language logo

      BST_LN_SUM

      0

      0

      avatar
      virendrakgarg

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.