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.
Input:9 / \ 2 4 / \ \ -1 3 0
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
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:
Time Complexity: O(N)
Auxiliary Space: O(N)
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.
- Sum of specially balanced nodes from a given Binary Tree
- Create Balanced Binary Tree using its Leaf Nodes without using extra space
- Count the Number of Binary Search Trees present in a Binary Tree
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- How to determine if a binary tree is height-balanced?
- Check if the Binary Tree contains a balanced BST of size K
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Count of all prime weight nodes between given nodes in the given Tree
- Prime Numbers present at Kth level of a Binary Tree
- Find the numbers present at Kth level of a Fibonacci Binary Tree
- Count Balanced Binary Trees of Height h
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Minimum number of groups of nodes such that no ancestor is present in the same group
- Program to count leaf nodes in a binary tree
- Iterative program to count leaf nodes in a Binary Tree
- Count half nodes in a Binary tree (Iterative and Recursive)
- Count full nodes in a Binary tree (Iterative and Recursive)
- Count Non-Leaf nodes in a Binary Tree
- Count of nodes in a Binary tree with immediate children as its factors
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.