Given a binary tree, you need to check whether sum of all covered elements is equal to sum of all uncovered elements or not.
In a binary tree, a node is called Uncovered if it appears either on left boundary or right boundary. Rest of the nodes are called covered.
For example, consider below binary tree
In above binary tree, Covered node: 6, 5, 7 Uncovered node: 9, 4, 3, 17, 22, 20 The output for this tree should be false as sum of covered and uncovered node is not same
We strongly recommend you to minimize your browser and try this yourself first.
For calculating sum of Uncovered nodes we will follow below steps:
1) Start from root, go to left and keep going until left child is available, if not go to right child and again follow same procedure until you reach a leaf node.
2) After step 1 sum of left boundary will be stored, now for right part again do the same procedure but now keep going to right until right child is available, if not then go to left child and follow same procedure until you reach a leaf node.
After above 2 steps sum of all Uncovered node will be stored, we can subtract it from total sum and get sum of covered elements and check for equines of binary tree.
C++
// C++ program to find sum of Covered and Uncovered node of // binary tree #include <bits/stdc++.h> using namespace std; /* A binary tree node has key, pointer to left child and a pointer to right child */struct Node { int key; struct Node* left, *right; }; /* To create a newNode of tree and return pointer */struct Node* newNode(int key) { Node* temp = new Node; temp->key = key; temp->left = temp->right = NULL; return (temp); } /* Utility function to calculate sum of all node of tree */int sum(Node* t) { if (t == NULL) return 0; return t->key + sum(t->left) + sum(t->right); } /* Recursive function to calculate sum of left boundary elements */int uncoveredSumLeft(Node* t) { /* If leaf node, then just return its key value */ if (t->left == NULL && t->right == NULL) return t->key; /* If left is available then go left otherwise go right */ if (t->left != NULL) return t->key + uncoveredSumLeft(t->left); else return t->key + uncoveredSumLeft(t->right); } /* Recursive function to calculate sum of right boundary elements */int uncoveredSumRight(Node* t) { /* If leaf node, then just return its key value */ if (t->left == NULL && t->right == NULL) return t->key; /* If right is available then go right otherwise go left */ if (t->right != NULL) return t->key + uncoveredSumRight(t->right); else return t->key + uncoveredSumRight(t->left); } // Returns sum of uncovered elements int uncoverSum(Node* t) { /* Initializing with 0 in case we don't have left or right boundary */ int lb = 0, rb = 0; if (t->left != NULL) lb = uncoveredSumLeft(t->left); if (t->right != NULL) rb = uncoveredSumRight(t->right); /* returning sum of root node, left boundry and right boundry*/ return t->key + lb + rb; } // Returns true if sum of covered and uncovered elements // is same. bool isSumSame(Node *root) { // Sum of uncovered elements int sumUC = uncoverSum(root); // Sum of all elements int sumT = sum(root); // Check if sum of covered and uncovered is same return (sumUC == (sumT - sumUC)); } /* Helper function to print inorder traversal of binary tree */void inorder(Node* root) { if (root) { inorder(root->left); printf("%d ", root->key); inorder(root->right); } } // Driver program to test above functions int main() { // Making above given diagram's binary tree Node* root = newNode(8); root->left = newNode(3); root->left->left = newNode(1); root->left->right = newNode(6); root->left->right->left = newNode(4); root->left->right->right = newNode(7); root->right = newNode(10); root->right->right = newNode(14); root->right->right->left = newNode(13); if (isSumSame(root)) printf("Sum of covered and uncovered is same\n"); else printf("Sum of covered and uncovered is not same\n"); } |
Java
// Java program to find sum of covered and uncovered nodes // of a binary tree /* A binary tree node has key, pointer to left child and a pointer to right child */class Node { int key; Node left, right; public Node(int key) { this.key = key; left = right = null; } } class BinaryTree { Node root; /* Utility function to calculate sum of all node of tree */ int sum(Node t) { if (t == null) return 0; return t.key + sum(t.left) + sum(t.right); } /* Recursive function to calculate sum of left boundary elements */ int uncoveredSumLeft(Node t) { /* If left node, then just return its key value */ if (t.left == null && t.right == null) return t.key; /* If left is available then go left otherwise go right */ if (t.left != null) return t.key + uncoveredSumLeft(t.left); else return t.key + uncoveredSumLeft(t.right); } /* Recursive function to calculate sum of right boundary elements */ int uncoveredSumRight(Node t) { /* If left node, then just return its key value */ if (t.left == null && t.right == null) return t.key; /* If right is available then go right otherwise go left */ if (t.right != null) return t.key + uncoveredSumRight(t.right); else return t.key + uncoveredSumRight(t.left); } // Returns sum of uncovered elements int uncoverSum(Node t) { /* Initializing with 0 in case we don't have left or right boundary */ int lb = 0, rb = 0; if (t.left != null) lb = uncoveredSumLeft(t.left); if (t.right != null) rb = uncoveredSumRight(t.right); /* returning sum of root node, left boundry and right boundry*/ return t.key + lb + rb; } // Returns true if sum of covered and uncovered elements // is same. boolean isSumSame(Node root) { // Sum of uncovered elements int sumUC = uncoverSum(root); // Sum of all elements int sumT = sum(root); // Check if sum of covered and uncovered is same return (sumUC == (sumT - sumUC)); } /* Helper function to print inorder traversal of binary tree */ void inorder(Node root) { if (root != null) { inorder(root.left); System.out.print(root.key + " "); inorder(root.right); } } // Driver program to test above functions public static void main(String[] args) { BinaryTree tree = new BinaryTree(); // Making above given diagram's binary tree tree.root = new Node(8); tree.root.left = new Node(3); tree.root.left.left = new Node(1); tree.root.left.right = new Node(6); tree.root.left.right.left = new Node(4); tree.root.left.right.right = new Node(7); tree.root.right = new Node(10); tree.root.right.right = new Node(14); tree.root.right.right.left = new Node(13); if (tree.isSumSame(tree.root)) System.out.println("Sum of covered and uncovered is same"); else System.out.println("Sum of covered and uncovered is not same"); } } // This code has been contributed by Mayank Jaiswal(mayank_24) |
Output :
Sum of covered and uncovered is not same
This article is contributed by Utkarsh Trivedi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- Tree Traversals (Inorder, Preorder and Postorder)
- Find the node with minimum value in a Binary Search Tree
- Write a program to Calculate Size of a tree | Recursion
- Write a Program to Find the Maximum Depth or Height of a Tree
- Write a program to Delete a Tree
- If you are given two traversal sequences, can you construct the binary tree?
- Convert a Binary Tree into its Mirror Tree
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Lowest Common Ancestor in a Binary Search Tree.
- The Great Tree-List Recursion Problem.
- Level Order Tree Traversal
- Program to count leaf nodes in a binary tree
- A program to check if a binary tree is BST or not
- Check for Children Sum Property in a Binary Tree
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
Improved By : MayankShorey




