

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find tree level that has minimum sum in C++
<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 −</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 – 10 = -6, which is minimum.</p><p>To solve this, we will follow these steps −</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 < 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−</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/9tXDRJBy" rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notranslate">#include <bits/stdc++.h> using namespace std; class TreeNode{ public: int val; TreeNode *left, *right; TreeNode(int data){ val = data; left = NULL; right = NULL; } }; class Solution { public: int solve(TreeNode* r) { int level = 1, sum = r->val; int ansLevel = level, ansSum = sum; queue <TreeNode*> q; q.push(r); while(!q.empty()){ int capacity = q.size(); level++; sum = 0; while(capacity--){ TreeNode* node = q.front(); q.pop(); if(node->right){ sum += node->right->val; q.push(node->right); } if(node->left){ sum += node->left->val; q.push(node->left); } } if(ansSum>sum){ ansSum = sum; ansLevel = level; } } return ansLevel; } }; main(){ TreeNode *root = new TreeNode(5); root->left = new TreeNode(4); root->right = new TreeNode(-10); root->left->right = new TreeNode(-2); root->right->left = new TreeNode(-7); root->right->right = new TreeNode(15); Solution ob; cout <<ob.solve(root); }</pre><h2>Input</h2><pre class="result notranslate">TreeNode *root = new TreeNode(5); root->left = new TreeNode(4); root->right = new TreeNode(-10); root->left->right = new TreeNode(-2); root->right->left = new TreeNode(-7); root->right->right = new TreeNode(15);</pre><h2>Output</h2><pre class="result notranslate">2</pre>
- Related Questions & Answers
- Find maximum level sum in Binary Tree in C++
- How to find the column that has the largest sum in R?
- Program to find minimum cost to increase heights of trees where no adjacent tree has same height in Python
- Program to Find Out the Minimum Parsing Tree in C++
- Maximum Level Sum of a Binary Tree in C++
- Program to find minimum absolute sum difference in Python
- Program to find out the path between two vertices in a graph that has the minimum penalty (Python)
- Find maximum level product in Binary Tree in C++
- C++ Program to Find the Minimum value of Binary Search Tree
- How to include a factor level in bar blot using ggplot2 in R if that level has a frequency zero.
- Program to find minimum digits sum of deleted digits in Python
- Program to find minimum largest sum of k sublists in C++
- Program to perform level order traversal of binary tree in C++
- Finding the sub array that has maximum sum JavaScript
- Program to find sum of all elements of a tree in Python
Advertisements