From 8bc9f20d006a9cead30e7060b761e2759ac8b7ba Mon Sep 17 00:00:00 2001 From: nehatiwariii <76935552+nehatiwariii@users.noreply.github.com> Date: Thu, 14 Oct 2021 23:30:09 +0530 Subject: [PATCH 1/2] Create print input tree level wise --- print input tree level wise | 82 +++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 print input tree level wise 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); +} From 385275f873355e92f4902ccfe1509b94ebe60108 Mon Sep 17 00:00:00 2001 From: nehatiwariii <76935552+nehatiwariii@users.noreply.github.com> Date: Thu, 14 Oct 2021 23:56:20 +0530 Subject: [PATCH 2/2] Create node with max childsum --- node with max childsum | 80 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 node with max childsum 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; + } +}