Given a Binary Tree, the task is to find the sum of all the specially balanced nodes in the given Binary Tree.
A specially balanced node in a Binary Tree contains the sum of nodes of one subtree(either left or right) as even and the sum of the other subtree as odd.
The nodes having only one child or no child can never be a balanced node.
Input: Below is the given Tree:
The specially balanced nodes are 11 and 22.
For Node 11:
The sum of left subtree is (23 + 13 + 9) = 45 which is Odd.
The sum of right subtree is (44 + 22 + 7 + 6 + 15) = 94 which is Even.
For Node 22:
The sum of left subtree is 6 which is Even.
The sum of right subtree is 15 which is Odd.
Therefore, the sum of specially balanced nodes is 11 + 22 = 33.
Input: Below is the given Tree:
The specially balanced nodes are 4 and 12.
For Node 4:
The sum of left subtree is 2 which is Even.
The sum of right subtree is 3 which is Odd.
For Node 12:
The sum of left subtree is 17 which is Odd.
The sum of right subtree is (16 + 4 + 9 + 2 + 3) = 34 which is Even.
Therefore, the sum of specially balanced nodes is 4 + 12 = 16.
Approach: The idea is to perform DFS Traversal on the given Tree using recursion and update the final sum as per the given conditions. Follow the steps below:
- Initialize a totalSum as 0 which stores the sum of all specially balanced nodes.
- Perform the DFS Traversal on the given Tree and check for the following:
- If the node is a leaf node, then return the value of that node.
- Now, if the current node is not a leaf node, recursively traverse for the left and right subtree.
- Return the value of the sum of the left and right subtree with the current root value when each recursive call ends.
- Check if the returned sums satisfy the property of the specially balanced node. IF found to be true, add the current node value to totalSum.
- Print the value of totalSum after completing the above steps.
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.
- Count balanced nodes present in a binary tree
- Create Balanced Binary Tree using its Leaf Nodes without using extra space
- 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
- Count of all prime weight nodes between given nodes in the given Tree
- How to determine if a binary tree is height-balanced?
- Check if the Binary Tree contains a balanced BST of size K
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Minimum difference between any two weighted nodes in Sum Tree of the given Tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Find sum of all nodes of the given perfect binary tree
- Maximum sum of leaf nodes among all levels of the given binary tree
- Sum of nodes in the right view of the given binary tree
- Sum of nodes in the left view of the given binary tree
- Maximum sum of non-leaf nodes among all levels of the given binary tree
- Check whether a given binary tree is skewed binary tree or not?
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Sum of all odd nodes in the path connecting two given nodes
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.