Count balanced nodes present in a binary tree
Given a binary tree, the task is to count the number of balanced nodes in the given tree.
Balanced nodes of a binary tree are defined as the nodes which contains both left and right subtrees with their respective sum of node values equal.
Examples:
Input:
9
/ \
2 4
/ \ \
-1 3 0
Output: 1
Explanation:
Only node 9 contains the left subtree sum = right subtree sum = 4
Therefore, the required output is 1.
Input:
7
/ \
4 10
/ \
3 3
/ \ \
0 0 -3
/
3
Output: 3
Approach: The idea is to recursively traverse every node of the given Binary Tree. For every node, calculate the sum of the nodes in the left and right subtree and check if the calculated sums are equal or not. If found to be true, increase count. Finally, print the count after complete traversal of the tree
Follow the steps below to solve the problem:
- Initialize a variable say, res, to store the count of balanced nodes
- Recursively calculate the sum of the left and right subtree for every node.
- Check if the calculated sums are equal or not.
- If found to be true, the current node is balanced. Therefore, increment res by 1
- Finally, print the value of res after complete traversal of the tree.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node( int val)
{
data = val;
left = right = NULL;
}
};
int Sum(Node* root, int & res)
{
if (root == NULL) {
return 0;
}
int leftSubSum
= Sum(root->left, res);
int rightSubSum
= Sum(root->right, res);
if (root->left and root->right
&& leftSubSum == rightSubSum)
res += 1;
return root->data + leftSubSum
+ rightSubSum;
}
int main()
{
Node* root = new Node(9);
root->left = new Node(2);
root->left->left = new Node(-1);
root->left->right = new Node(3);
root->right = new Node(4);
root->right->right = new Node(0);
int res = 0;
Sum(root, res);
cout << res;
}
|
Java
class GFG{
static int res = 0 ;
static class Node
{
int data;
Node left;
Node right;
Node( int val)
{
data = val;
left = right = null ;
}
};
static int Sum(Node root)
{
if (root == null )
{
return 0 ;
}
int leftSubSum = Sum(root.left);
int rightSubSum = Sum(root.right);
if (root.left != null && root.right != null &&
leftSubSum == rightSubSum)
res += 1 ;
return root.data + leftSubSum +
rightSubSum;
}
public static void main(String[] args)
{
Node root = new Node( 9 );
root.left = new Node( 2 );
root.left.left = new Node(- 1 );
root.left.right = new Node( 3 );
root.right = new Node( 4 );
root.right.right = new Node( 0 );
res = 0 ;
Sum(root);
System.out.print(res);
}
}
|
Python3
class Node:
def __init__( self , val):
self .data = val
self .left = None
self .right = None
def Sum (root):
global res
if (root = = None ):
return 0
leftSubSum = Sum (root.left)
rightSubSum = Sum (root.right)
if (root.left and root.right and
leftSubSum = = rightSubSum):
res + = 1
return (root.data + leftSubSum +
rightSubSum)
root = Node( 9 )
root.left = Node( 2 )
root.left.left = Node( - 1 )
root.left.right = Node( 3 )
root.right = Node( 4 )
root.right.right = Node( 0 )
global res
res = 0
Sum (root)
print (res)
|
C#
using System;
class GFG{
static int res = 0;
public class Node
{
public int data;
public Node left;
public Node right;
public Node( int val)
{
data = val;
left = right = null ;
}
};
static int Sum(Node root)
{
if (root == null )
{
return 0;
}
int leftSubSum = Sum(root.left);
int rightSubSum = Sum(root.right);
if (root.left != null && root.right != null &&
leftSubSum == rightSubSum)
res += 1;
return root.data + leftSubSum +
rightSubSum;
}
public static void Main(String[] args)
{
Node root = new Node(9);
root.left = new Node(2);
root.left.left = new Node(-1);
root.left.right = new Node(3);
root.right = new Node(4);
root.right.right = new Node(0);
res = 0;
Sum(root);
Console.Write(res);
}
}
|
Javascript
<script>
let res = 0;
class Node
{
constructor(val) {
this .left = null ;
this .right = null ;
this .data = val;
}
}
function Sum(root)
{
if (root == null )
{
return 0;
}
let leftSubSum = Sum(root.left);
let rightSubSum = Sum(root.right);
if (root.left != null && root.right != null &&
leftSubSum == rightSubSum)
res += 1;
return root.data + leftSubSum +
rightSubSum;
}
let root = new Node(9);
root.left = new Node(2);
root.left.left = new Node(-1);
root.left.right = new Node(3);
root.right = new Node(4);
root.right.right = new Node(0);
res = 0;
Sum(root);
document.write(res);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
21 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...