Given a binary tree, the task is to find the sum of all the nodes whose parent is even.
Input: 1 / \ 3 8 / \ 5 6 / 1 Output: 11 The only even nodes are 8 and 6 and the sum of their children is 5 + 6 = 11. Input: 2 / \ 3 8 / / \ 2 5 6 / \ 1 3 Output: 25 3 + 8 + 5 + 6 + 3 = 25
Approach: Initialse sum = 0 and perform a recursive traversal of the tree and check if the node is even or not, if the node is even then add the values of its children to the sum. Finally, print the sum.
Below is the implementation of the above approach:
Time Complexity: O(n) where n is number of nodes in the given Binary Tree.
- Sum of all parent-child differences in a Binary Tree
- Count all Grandparent-Parent-Child Triplets in a binary tree whose sum is greater than X
- Sum of all the parent nodes having child node x
- Ways to color a skewed tree such that parent and child have different colors
- Distance between two nodes of binary tree with node values from 1 to N
- Maximum parent children sum in Binary tree
- Find the parent of a node in the given binary tree
- Find right sibling of a binary tree with parent pointers
- Construct Binary Tree from given Parent Array representation
- Lowest Common Ancestor in a Binary Tree | Set 2 (Using Parent Pointer)
- Find Height of Binary Tree represented by Parent array
- Construct Binary Tree from given Parent Array representation | Iterative Approach
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Longest Path with Same Values in a Binary Tree
- Check if a Binary Tree (not BST) has duplicate values
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : AnkitRai01