Given a binary tree, find the sum of all parent-child differences for all the non-leaf nodes of the given binary tree.
Note that parent-child difference is (parent node’s value – (sum of child node’s values)).
Input: 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Output: -23 1st parent-child difference = 1 -(2 + 3) = -4 2nd parent-child difference = 2 -(4 + 5) = -7 3rd parent-child difference = 3 -(6 + 7) = -10 4th parent-child difference = 6 - 8 = -2 Total sum = -23 Input: 1 / \ 2 3 \ / 5 6 Output: -10
Naive Approach: The idea is to traverse the tree in any fashion and check if the node is the leaf node or not. If the node is non-leaf node, add (node data – sum of children node data) to result.
Efficient Approach: In the final result, a close analysis suggests that each internal node ( nodes which are neither root nor leaf) once gets treated as a child and once as a parent hence their contribution in the final result is zero. Also, the root is only treated as a parent once and in a similar fashion, all leaf nodes are treated as children once. Hence, the final result is (value of root – sum of all leaf nodes).
Below is the implementation of the above approach:
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
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Convert a Generic Tree(N-array Tree) to Binary Tree
- Check if a binary tree is subtree of another binary tree | Set 1
- Binary Tree to Binary Search Tree Conversion
- Check if a binary tree is subtree of another binary tree | Set 2
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Check whether a binary tree is a full binary tree or not
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Minimum swap required to convert binary tree to binary search tree
- Check whether a given binary tree is skewed binary tree or not?
- Difference between Binary Tree and Binary Search Tree
- Binary Tree | Set 3 (Types of Binary Tree)
- 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
- Binary Tree to Binary Search Tree Conversion using STL set
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Find sum of all left leaves in a given Binary Tree
- Change a Binary Tree so that every node stores sum of all nodes in left subtree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.