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 sum of data values in left and right children. Consider data value as 0 for NULL children. Below tree is an example
Traverse the given binary tree. For each node check (recursively) if the node and both its children satisfy the Children Sum Property, if so then return true else return false.
The given tree satisfies the children sum property
Time Complexity: O(n), we are doing a complete traversal of the tree.
As an exercise, extend the above question for an n-ary tree.
This question was asked by Shekhar.
Please write comments if you find any bug in the above algorithm or a better way to solve the same problem.
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.
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Iterative approach to check for children sum property in a Binary Tree
- 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
- Find root of the tree where children id sum for every node is given
- Node having maximum sum of immediate children and itself in n-ary tree
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Given a n-ary tree, count number of nodes which have more number of children than parents
- Number of children of given node in n-ary Tree
- Number of full binary trees such that each node is product of its children
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Check if a binary tree is subtree of another binary tree | Set 1
- Check if a binary tree is subtree of another binary tree | Set 2
- Check whether a binary tree is a full binary tree or not
- Check whether a given binary tree is skewed binary tree or not?
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Check whether a binary tree is a full binary tree or not | Iterative Approach