From 1a6960a8fe8241fc73a53bfd006ac8bb84dc4e51 Mon Sep 17 00:00:00 2001 From: Chitra Singla <45913363+chitrasingla@users.noreply.github.com> Date: Wed, 13 Oct 2021 20:02:16 +0530 Subject: [PATCH] Create Find Sum at Level K --- Tree/Find Sum at Level K | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Tree/Find Sum at Level K diff --git a/Tree/Find Sum at Level K b/Tree/Find Sum at Level K new file mode 100644 index 0000000..94bf137 --- /dev/null +++ b/Tree/Find Sum at Level K @@ -0,0 +1,58 @@ +#include +using namespace std; +class node{ + public: + int data; + node* left; + node* right; + + node(int d){ + data = d; + left = right = NULL; + } +}; +node* buildTree(){ + int d,c; + cin>>d>>c; + node* root = new node(d); + if(c==0) + { + + } + else if(c==1) + { + root->left = buildTree(); + } + else if(c==2) + { + root->left = buildTree(); + root->right = buildTree(); + } + return root; + + +} +void findSum(node* root , int k, int &sum){ + + if(root==NULL) + return; + + if(k==0){ + sum += root->data; + return; + } + findSum(root->left,k-1,sum); + findSum(root->right,k-1,sum); + +} + + + +int main(){ + node* root = buildTree(); + int k; + cin>>k; + int sum=0; + findSum(root,k,sum); + cout<