Skip to content

Instantly share code, notes, and snippets.

@ajinkyajawale14499
Created August 6, 2019 11:05
Show Gist options
  • Save ajinkyajawale14499/8a539464de2e5a8b67b949479d0cfc7f to your computer and use it in GitHub Desktop.
Save ajinkyajawale14499/8a539464de2e5a8b67b949479d0cfc7f to your computer and use it in GitHub Desktop.
Sum of nodes at maximum depth of a Binary Tree
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int sum;
int maxlevel=INT_MIN;
class node{
public: int data;
node* left;
node* right;
node(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
void sumdepth(node* node,int level){
if(node==NULL) return ;
/*
else{
int ldepth=maxdepth(node->left);
int rdepth=maxdepth(node->right);
if(ldepth>rdepth) return(ldepth+1);
else return(rdepth+1);
}
*/
if(level>maxlevel){
sum=node->data;
maxlevel=level;
}
else if(level==maxlevel)
{
sum=sum+node->data;
}
}
int main()
{
node* root = new node(4);
root->left = new node(2);
root->right = new node(5);
root->left->right = new node(1);
root->left->right=new node(3);
sumdepth(root,0);
cout<<"max depth of tree is"<<sum;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment