diff --git a/node with max childsum b/node with max childsum new file mode 100644 index 00000000..19725293 --- /dev/null +++ b/node with max childsum @@ -0,0 +1,80 @@ +#include +#include +#include +using namespace std; + +template +class TreeNode { + public: + T data; + vector*> children; + + TreeNode(T data) { this->data = data; } + + ~TreeNode() { + for (int i = 0; i < children.size(); i++) { + delete children[i]; + } + } +}; + + +TreeNode* takeInputLevelWise() { + int rootData; + cin >> rootData; + TreeNode* root = new TreeNode(rootData); + + queue*> pendingNodes; + + pendingNodes.push(root); + while (pendingNodes.size() != 0) { + TreeNode* front = pendingNodes.front(); + pendingNodes.pop(); + int numChild; + cin >> numChild; + for (int i = 0; i < numChild; i++) { + int childData; + cin >> childData; + TreeNode* child = new TreeNode(childData); + front->children.push_back(child); + pendingNodes.push(child); + } + } + + return root; +} +TreeNode* maxSumNode(TreeNode* root) +{ + if(root==NULL) + return NULL; + int sum=root->data; + for(int i=0;ichildren.size();i++) + { + sum=sum+root->children[i]->data; + } + TreeNode*ans=root; + for(int i=0;ichildren.size();i++) + { + TreeNode*max=maxSumNode(root->children[i]); + int q=max->data; + + for(int i=0;ichildren.size();i++){ + q+=max->children[i]->data; + } + if(q>sum){ + sum=q; + ans=max; + } + }return ans; +} + + +int main() { + TreeNode* root = takeInputLevelWise(); + + TreeNode* ans = maxSumNode(root); + + if (ans != NULL) { + cout << ans->data; + } +} diff --git a/print input tree level wise b/print input tree level wise new file mode 100644 index 00000000..63dedefc --- /dev/null +++ b/print input tree level wise @@ -0,0 +1,82 @@ +#include +#include +#include +using namespace std; + +template +class TreeNode { + public: + T data; + vector*> children; + + TreeNode(T data) { this->data = data; } + + ~TreeNode() { + for (int i = 0; i < children.size(); i++) { + delete children[i]; + } + } +}; + + +TreeNode* takeInputLevelWise() { + int rootData; + cin >> rootData; + TreeNode* root = new TreeNode(rootData); + + queue*> pendingNodes; + + pendingNodes.push(root); + while (pendingNodes.size() != 0) { + TreeNode* front = pendingNodes.front(); + pendingNodes.pop(); + int numChild; + cin >> numChild; + for (int i = 0; i < numChild; i++) { + int childData; + cin >> childData; + TreeNode* child = new TreeNode(childData); + front->children.push_back(child); + pendingNodes.push(child); + } + } + + return root; +} +#include +void printLevelWise(TreeNode* root) { + queue*>pendingnodes; + pendingnodes.push(root); + while(pendingnodes.size()!=0) + { + TreeNode*Front=pendingnodes.front(); + pendingnodes.pop(); + cout<data<<":"; + if(Front->children.size()==0) + cout<children.size();i++) + { + if(i==Front->children.size()-1) + cout<children[i]->data<children[i]->data<<","; + } + TreeNode* child= Front->children[i]; + pendingnodes.push(child); + + } + } + + } + + +} + + +int main() { + TreeNode* root = takeInputLevelWise(); + printLevelWise(root); +}