Check for Children-Sum property in Binary Tree
Check for Children-sum property in binary tree
In this article, we will learn the approach and code about how to check for the children-sum property in binary tree in C++. Children sum property is true if the sum of the value of left node and right node is equal to the parent node value.
As an Input We are Given a tree and we have to check for children sum property. Whether that property if followed in an entire tree or not.
Note:- Deviation of even single Node from above property will result in a negative answer.
Children-Sum Property:–
This property says that for each node sum of its left and right children should be equal to node value.
Also, following assumptions are to be kept in mind while recursively traversing tree
- A leaf node satisfies children sum property because leaf nodes don’t have any child nodes.
- An Empty tree satisfies Children sum property.
Algorithm:-
Step1:- Traverse the tree.
Step 2:- For every node in tree check whether the value in root node equals the sum of it lchild and rchild.
If yes continue from Step 1 Untill root==NULL
If No return false
The time complexity for above program is O(n). Since each node is covered once.
Code Implementation to check for Children-sum property in binary tree in C++
#include<bits/stdc++.h> using namespace std; struct Treenode { int val; Treenode *lchild; Treenode *rchild; }; struct Treenode *newNode (int key) { struct Treenode *newnode = (struct Treenode *) malloc (sizeof (struct Treenode)); newnode->val = key; newnode->lchild = NULL; newnode->rchild = NULL; return (newnode); } int TreefollowingChildSumProperty (Treenode * head) { int lchild_val = 0, rchild_val = 0; if (head == NULL || (head->lchild == NULL && head->rchild == NULL)) return 1; else { if (head->lchild != NULL) lchild_val = head->lchild->val; if (head->rchild != NULL) rchild_val = head->rchild->val; if ((head->val == lchild_val + rchild_val) && TreefollowingChildSumProperty (head->lchild) && TreefollowingChildSumProperty (head->rchild)) return 1; else return 0; } } int main () { Treenode *root1 = newNode (232); Treenode *root2 = newNode (232); root1->lchild = newNode (231); root1->rchild = newNode (231); root1->lchild->lchild = newNode (431); root1->lchild->rchild = newNode (531); if (TreefollowingChildSumProperty (root1)) cout << "Given Tree Follows children Sum Property\n"; else cout << "Tree does not follows Children Sum property\n"; getchar (); return 0; }
Output: Tree does not follows Children Sum Property
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
Binary Search Trees
Traversals
- Traversal in Trees
- Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
- Tree Traversals: Depth First Search (DFS) : C | C++ | Java
- Construct a Binary Tree from Postorder and Inorder
B – Trees
AVL Trees
- AVL Trees
Complete Programs for Trees
- Depth First Traversals – C | C++ | Java
- Level Order Traversal – C | C++ | Java
- Construct Tree from given Inorder and Preorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Inorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Preorder traversal – C | C++ | Java
- Find size of the Binary tree – C | C++ | Java
- Find the height of binary tree – C | C++ | Java
- Find maximum in binary tree – C | C++ | Java
- Check whether two tree are identical- C| C++| Java
- Spiral Order traversal of Tree- C | C++| Java
- Level Order Traversal Line by Line – C | C++| Java
- Hand shaking lemma and some Impotant Tree Properties.
- Check If binary tree if Foldable or not.- C| C++| Java
- check whether tree is Symmetric – C| C++| Java.
- Check for Children-Sum in Binary Tree- C|C++| Java
- Sum of all nodes in Binary Tree- C | C++ | Java
- Lowest Common Ancestor in Binary Tree- C | C++ | Java
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
Binary Search Trees
Traversals
- Traversal in Trees
- Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
- Tree Traversals: Depth First Search (DFS) : C | C++ | Java
- Construct a Binary Tree from Postorder and Inorder
B – Trees
AVL Trees
- AVL Trees
Complete Programs for Trees
- Depth First Traversals – C | C++ | Java
- Level Order Traversal – C | C++ | Java
- Construct Tree from given Inorder and Preorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Inorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Preorder traversal – C | C++ | Java
- Find size of the Binary tree – C | C++ | Java
- Find the height of binary tree – C | C++ | Java
- Find maximum in binary tree – C | C++ | Java
- Check whether two tree are identical- C| C++| Java
- Spiral Order traversal of Tree- C | C++| Java
- Level Order Traversal LIne by Line – C | C++| Java
- Hand shaking lemma and some Impotant Tree Properties.
- Check If binary tree if Foldable or not.- C| C++| Java
- check whether tree is Symmetric C| C++| Java.
- Check for Children-Sum in Binary Tree- C|C++| Java
- Sum of all nodes in Binary Tree- C | C++ | Java
- Lowest Common Ancestor in Binary Tree. C | C++ | Java
Login/Signup to comment