Given a Binary Tree, the task is to check if the maximum sum level divides the binary tree into the two parts of two equal sum halves.
Examples:
Input:
1
/ \
2 3
/ \ \
4 5 8
/ \
2 4
Output: YES
Explanation:
The maximum sum level is 2 and
its sum is (4 + 5 + 8 = 17)
Sum of the upper half (1 + 2 + 3) = 6
Sum of the Lower half (2 + 4) = 6
Input:
10
/ \
20 30
/ \ \
4 5 1
Output: YES
Explanation:
The maximum sum level is 1 and
its sum is (20 + 30 = 50)
Sum of the upper half (10) = 10
Sum of the lower half (5 + 4 + 1) = 10
Approach: The idea is to use level order traversal to compute the sum of every level of the binary tree. Then, find the maximum sum of all the levels. Finally, check that the total sum of all the levels less than the maximum level sum is equal to the total sum of the levels of the greater than the maximum level sum.
Below is the implementation of the above approach:
C++
// C++ implementation to check if// maximum level sum divides the// Binary tree into two equal sum halves#include <bits/stdc++.h>using namespace std;// Structure of the nodestruct Node { int data; struct Node *left, *right;};// Utility function to// create a new nodestruct Node* newNode(int x){ struct Node* temp = new Node; temp->data = x; temp->left = temp->right = NULL; return temp;};// Function to check if// maximum level sum divides the// Binary tree into two equal sum halvesbool check_horizontal(struct Node* root){ // Vector used to store the sum // of all levels of the Binary Tree vector<int> sumLevel; // In index variable we store the // level of the maximum level sum int index = -1, maxSum = 0, level = 0; queue<Node*> q; q.push(root); while (!q.empty()) { // Varible to store the // current level sum. int sum = 0; // Size of the Queue int n = q.size(); // Loop to iterate over the // elements nodes of current level for (int i = 0; i < n; i++) { // Inserting the next level // elements to the Queue Node* temp = q.front(); sum += temp->data; if (temp->left != NULL) q.push(temp->left); if (temp->right != NULL) q.push(temp->right); // Popping out the current // level element from the Queue q.pop(); } // Storing the current level // sum into the vector sumLevel.push_back(sum); // Level of maximum // horizontal sum line if (sum > maxSum) { maxSum = sum; index = level; } level++; } // Find the left half and right // half sum and check if they are equal int leftSum = 0, rightSum = 0; for (int i = 0; i < index; i++) { leftSum += sumLevel[i]; } for (int i = index + 1; i < sumLevel.size(); i++) { rightSum += sumLevel[i]; } return (leftSum == rightSum);}// Driver Codeint main(){ struct Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); root->right->right = newNode(8); root->right->right->left = newNode(2); root->right->right->right = newNode(4); // Condition to check if the // maxumum sum level divides // it into two equal half if (check_horizontal(root)) cout << "YES" << endl; else cout << "NO" << endl;} |
Java
// Java implementation to check if// maximum level sum divides the// Binary tree into two equal sum halvesimport java.util.*;class GFG{// Structure of the nodestatic class Node { int data; Node left, right;};// Utility function to// create a new nodestatic Node newNode(int x){ Node temp = new Node(); temp.data = x; temp.left = temp.right = null; return temp;};// Function to check if maximum// level sum divides the Binary// tree into two equal sum halvesstatic boolean check_horizontal(Node root){ // Vector used to store the sum // of all levels of the Binary Tree Vector<Integer> sumLevel = new Vector<Integer>(); // In index variable we store the // level of the maximum level sum int index = -1, maxSum = 0, level = 0; Queue<Node> q = new LinkedList<Node>(); q.add(root); while (!q.isEmpty()) { // Varible to store the // current level sum. int sum = 0; // Size of the Queue int n = q.size(); // Loop to iterate over the // elements nodes of current level for(int i = 0; i < n; i++) { // Inserting the next level // elements to the Queue Node temp = q.peek(); sum += temp.data; if (temp.left != null) q.add(temp.left); if (temp.right != null) q.add(temp.right); // Popping out the current // level element from the Queue q.remove(); } // Storing the current level // sum into the vector sumLevel.add(sum); // Level of maximum // horizontal sum line if (sum > maxSum) { maxSum = sum; index = level; } level++; } // Find the left half and right // half sum and check if they are equal int leftSum = 0, rightSum = 0; for(int i = 0; i < index; i++) { leftSum += sumLevel.get(i); } for(int i = index + 1; i < sumLevel.size(); i++) { rightSum += sumLevel.get(i); } return (leftSum == rightSum);}// Driver Codepublic static void main(String[] args){ Node root = newNode(1); root.left = newNode(2); root.right = newNode(3); root.left.left = newNode(4); root.left.right = newNode(5); root.right.right = newNode(8); root.right.right.left = newNode(2); root.right.right.right = newNode(4); // Condition to check if the // maxumum sum level divides // it into two equal half if (check_horizontal(root)) System.out.print("YES" + "\n"); else System.out.print("NO" + "\n");}}// This code is contributed by Amit Katiyar |
C#
// C# implementation to check if// maximum level sum divides the// Binary tree into two equal sum halvesusing System;using System.Collections.Generic;class GFG{// Structure of // the nodepublic class Node { public int data; public Node left, right;};// Utility function to// create a new nodestatic Node newNode(int x){ Node temp = new Node(); temp.data = x; temp.left = temp.right = null; return temp;}// Function to check if maximum// level sum divides the Binary// tree into two equal sum halvesstatic bool check_horizontal(Node root){ // List used to store the sum // of all levels of the Binary Tree List<int> sumLevel = new List<int>(); // In index variable we store the // level of the maximum level sum int index = -1, maxSum = 0, level = 0; Queue<Node> q = new Queue<Node>(); q.Enqueue(root); while (q.Count != 0) { // Varible to store the // current level sum. int sum = 0; // Size of the Queue int n = q.Count; // Loop to iterate over the // elements nodes of current level for(int i = 0; i < n; i++) { // Inserting the next level // elements to the Queue Node temp = q.Peek(); sum += temp.data; if (temp.left != null) q.Enqueue(temp.left); if (temp.right != null) q.Enqueue(temp.right); // Popping out the current // level element from the Queue q.Dequeue(); } // Storing the current level // sum into the vector sumLevel.Add(sum); // Level of maximum // horizontal sum line if (sum > maxSum) { maxSum = sum; index = level; } level++; } // Find the left half and right // half sum and check if they are equal int leftSum = 0, rightSum = 0; for(int i = 0; i < index; i++) { leftSum += sumLevel[i]; } for(int i = index + 1; i < sumLevel.Count; i++) { rightSum += sumLevel[i]; } return (leftSum == rightSum);}// Driver Codepublic static void Main(String[] args){ Node root = newNode(1); root.left = newNode(2); root.right = newNode(3); root.left.left = newNode(4); root.left.right = newNode(5); root.right.right = newNode(8); root.right.right.left = newNode(2); root.right.right.right = newNode(4); // Condition to check if the // maxumum sum level divides // it into two equal half if (check_horizontal(root)) Console.Write("YES" + "\n"); else Console.Write("NO" + "\n");}}// This code is contributed by Rajput-Ji |
YES
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Number of ways to divide a Binary tree into two halves
- Maximum cost of splitting given Binary Tree into two halves
- Check if removing an edge can divide a Binary Tree in two halves
- Difference between sums of odd level and even level nodes of a Binary Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Print the nodes corresponding to the level value for each level of a Binary Tree
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Maximum absolute difference between any two level sum in a Binary Tree
- Min-Max Product Tree of a given Binary Tree
- Print nodes between two given level numbers of a binary tree
- Count nodes with two children at level L in a Binary Tree
- Connect Nodes at same Level (Level Order Traversal)
- Check if the given array can represent Level Order Traversal of Binary Search Tree
- Check if a binary tree is sorted level-wise or not
- Check if value exists in level-order sorted complete binary tree
- Check if the level order traversal of a Binary Tree results in a palindrome
- Check if a Tree can be split into K equal connected components
- Print Binary Search Tree in Min Max Fashion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

