Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions node with max childsum
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

template <typename T>
class TreeNode {
public:
T data;
vector<TreeNode<T>*> children;

TreeNode(T data) { this->data = data; }

~TreeNode() {
for (int i = 0; i < children.size(); i++) {
delete children[i];
}
}
};


TreeNode<int>* takeInputLevelWise() {
int rootData;
cin >> rootData;
TreeNode<int>* root = new TreeNode<int>(rootData);

queue<TreeNode<int>*> pendingNodes;

pendingNodes.push(root);
while (pendingNodes.size() != 0) {
TreeNode<int>* front = pendingNodes.front();
pendingNodes.pop();
int numChild;
cin >> numChild;
for (int i = 0; i < numChild; i++) {
int childData;
cin >> childData;
TreeNode<int>* child = new TreeNode<int>(childData);
front->children.push_back(child);
pendingNodes.push(child);
}
}

return root;
}
TreeNode<int>* maxSumNode(TreeNode<int>* root)
{
if(root==NULL)
return NULL;
int sum=root->data;
for(int i=0;i<root->children.size();i++)
{
sum=sum+root->children[i]->data;
}
TreeNode<int>*ans=root;
for(int i=0;i<root->children.size();i++)
{
TreeNode<int>*max=maxSumNode(root->children[i]);
int q=max->data;

for(int i=0;i<max->children.size();i++){
q+=max->children[i]->data;
}
if(q>sum){
sum=q;
ans=max;
}
}return ans;
}


int main() {
TreeNode<int>* root = takeInputLevelWise();

TreeNode<int>* ans = maxSumNode(root);

if (ans != NULL) {
cout << ans->data;
}
}
82 changes: 82 additions & 0 deletions print input tree level wise
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

template <typename T>
class TreeNode {
public:
T data;
vector<TreeNode<T>*> children;

TreeNode(T data) { this->data = data; }

~TreeNode() {
for (int i = 0; i < children.size(); i++) {
delete children[i];
}
}
};


TreeNode<int>* takeInputLevelWise() {
int rootData;
cin >> rootData;
TreeNode<int>* root = new TreeNode<int>(rootData);

queue<TreeNode<int>*> pendingNodes;

pendingNodes.push(root);
while (pendingNodes.size() != 0) {
TreeNode<int>* front = pendingNodes.front();
pendingNodes.pop();
int numChild;
cin >> numChild;
for (int i = 0; i < numChild; i++) {
int childData;
cin >> childData;
TreeNode<int>* child = new TreeNode<int>(childData);
front->children.push_back(child);
pendingNodes.push(child);
}
}

return root;
}
#include<queue>
void printLevelWise(TreeNode<int>* root) {
queue<TreeNode<int>*>pendingnodes;
pendingnodes.push(root);
while(pendingnodes.size()!=0)
{
TreeNode<int>*Front=pendingnodes.front();
pendingnodes.pop();
cout<<Front->data<<":";
if(Front->children.size()==0)
cout<<endl;
else
{
for(int i=0;i<Front->children.size();i++)
{
if(i==Front->children.size()-1)
cout<<Front->children[i]->data<<endl;
else
{
cout<<Front->children[i]->data<<",";
}
TreeNode<int>* child= Front->children[i];
pendingnodes.push(child);

}
}

}


}


int main() {
TreeNode<int>* root = takeInputLevelWise();
printLevelWise(root);
}