Program to find tree level that has minimum sum in C++

C++Server Side ProgrammingProgramming

<p>Suppose we have a binary tree, the level of its root is 1, the level of its children is 2, and so on.We have to find the smallest level X such that the sum of all the values of nodes at level X is minimum. So if the tree is like &minus;</p><p><img src="https://www.tutorialspoint.com/assets/questions/media/44077/tree_level.jpg" class="fr-fic fr-dib" width="215" height="228"></p><p>Output will be 2 as the sum is 4 &ndash; 10 = -6, which is minimum.</p><p>To solve this, we will follow these steps &minus;</p><ul class="list"><li><p>level := 1, sum := value of r, ansLevel := level, ansSum := sum</p></li><li><p>define a queue q, insert given node r into q</p></li><li><p>while q is not empty</p><ul class="list"><li><p>capacity := size of q</p></li><li><p>increase level by 1, sum := 0</p></li><li><p>while capacity is not 0</p><ul class="list"><li><p>node := front node from q, delete node from q</p></li><li><p>if right of node is valid, then sum := sum + value of right node, insert right</p></li><li>node into q</li><li><p>if left of node is valid, then sum := sum + value of left node, insert left node into q</p></li><li><p>decrease capacity by 1</p></li></ul></li><li><p>if ansSum &lt; sum, then ansSum := sum, ansLevel := level</p></li></ul></li><li><p>return ansLevel</p></li></ul><p>Let us see the following implementation to get better understanding&minus;</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/9tXDRJBy" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">#include &lt;bits/stdc++.h&gt; using namespace std; class TreeNode{ &nbsp; &nbsp;public: &nbsp; &nbsp; &nbsp; int val; &nbsp; &nbsp; &nbsp; TreeNode *left, *right; &nbsp; &nbsp; &nbsp; TreeNode(int data){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;val = data; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;left = NULL; &nbsp; &nbsp; &nbsp; right = NULL; &nbsp; &nbsp; &nbsp; } }; class Solution { &nbsp; &nbsp;public: &nbsp; &nbsp;int solve(TreeNode* r) { &nbsp; &nbsp; &nbsp; int level = 1, sum = r-&gt;val; &nbsp; &nbsp; &nbsp; int ansLevel = level, ansSum = sum; &nbsp; &nbsp; &nbsp; queue &lt;TreeNode*&gt; q; &nbsp; &nbsp; &nbsp; q.push(r); &nbsp; &nbsp; &nbsp; while(!q.empty()){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int capacity = q.size(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;level++; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sum = 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while(capacity--){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TreeNode* node = q.front(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; q.pop(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(node-&gt;right){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sum += node-&gt;right-&gt;val; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;q.push(node-&gt;right); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(node-&gt;left){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sum += node-&gt;left-&gt;val; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;q.push(node-&gt;left); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(ansSum&gt;sum){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansSum = sum; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansLevel = level; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; return ansLevel; &nbsp; &nbsp;} }; main(){ &nbsp; &nbsp;TreeNode *root = new TreeNode(5); &nbsp; &nbsp;root-&gt;left = new TreeNode(4); &nbsp; &nbsp;root-&gt;right = new TreeNode(-10); &nbsp; &nbsp;root-&gt;left-&gt;right = new TreeNode(-2); &nbsp; &nbsp;root-&gt;right-&gt;left = new TreeNode(-7); &nbsp; &nbsp;root-&gt;right-&gt;right = new TreeNode(15); &nbsp; &nbsp;Solution ob; &nbsp; &nbsp;cout &lt;&lt;ob.solve(root); }</pre><h2>Input</h2><pre class="result notranslate">TreeNode *root = new TreeNode(5); root-&gt;left = new TreeNode(4); root-&gt;right = new TreeNode(-10); root-&gt;left-&gt;right = new TreeNode(-2); root-&gt;right-&gt;left = new TreeNode(-7); root-&gt;right-&gt;right = new TreeNode(15);</pre><h2>Output</h2><pre class="result notranslate">2</pre>
raja
Updated on 10-Oct-2020 11:18:23

Advertisements