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.
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
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.
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Check if all nodes of the Binary Tree can be represented as sum of two primes
- Check if a Binary Tree consists of a pair of leaf nodes with sum K
- 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 binary tree is a full binary tree or not | Iterative Approach
- 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
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Difference between sum of even and odd valued nodes in a Binary Tree
- Check if two nodes are cousins in a Binary Tree
- Check if two nodes are cousins in a Binary Tree | Set-2
- Check if two nodes in a Binary Tree are siblings
- Check whether nodes of Binary Tree form Arithmetic, Geometric or Harmonic Progression
- Check if all the Nodes in a Binary Tree having common values are at least D distance apart