Given a binary tree, write a function that returns true if the tree satisfies below property:
For every node, data value must be equal to the sum of data values in left and right children. Consider data value as 0 for NULL children.
Input : 10 / \ 8 2 / \ \ 3 5 2 Output : Yes Input : 5 / \ -2 7 / \ \ 1 6 7 Output : No
We have already discussed the recursive approach. In this post an iterative approach is discussed.
Approach: The idea is to use a queue to do level order traversal of the Binary Tree and simultaneously check for every node:
- If the current node has two children and if the current node is equal to the sum of its left and right children.
- If the current node has just left child and if the current node is equal to its left child.
- If the current node has just right child and if the current node is equal to its right child.
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check for Symmetric Binary Tree (Iterative Approach)
- Iterative approach to check if a Binary Tree is Perfect
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Check for Children Sum Property in a Binary Tree
- Get level of a node in binary tree | iterative approach
- Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
- Deepest right leaf node in a binary tree | Iterative approach
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Iterative Approach to check if two Binary Trees are Isomorphic or not
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Deepest left leaf node in a binary tree | iterative approach
- Construct Binary Tree from given Parent Array representation | Iterative Approach
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Check if a Binary Tree is BST : Simple and Efficient Approach
- Maximum parent children sum in Binary tree
- Count nodes with two children at level L in a Binary Tree
- Count of nodes in a Binary tree with immediate children as its factors
- Count of nodes in a Binary Tree whose immediate children are co-prime
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.