Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
Question: Given an arbitrary binary tree, convert it to a binary tree that holds Children Sum Property. You can only increment data values in any node (You cannot change the structure of the tree and cannot decrement the value of any node).
For example, the below tree doesn’t hold the children sum property, convert it to a tree that holds the property.
50 / \ / \ 7 2 / \ /\ / \ / \ 3 5 1 30
Traverse the given tree in post order to convert it, i.e., first change left and right children to hold the children sum property then change the parent node.
Let difference between node’s data and children sum be diff.
diff = node’s children sum - node’s data
If diff is 0 then nothing needs to be done.
If diff > 0 ( node’s data is smaller than node’s children sum) increment the node’s data by diff.
If diff < 0 (node’s data is greater than the node’s children sum) then increment one child’s data. We can choose to increment either left or right child if they both are not NULL. Let us always first increment the left child. Incrementing a child changes the subtree’s children sum property so we need to change left subtree also. So we recursively increment the left child. If left child is empty then we recursively call increment() for right child.
Let us run the algorithm for the given example.
First convert the left subtree (increment 7 to 8).
50 / \ / \ 8 2 / \ /\ / \ / \ 3 5 1 30
Then convert the right subtree (increment 2 to 31)
50 / \ / \ 8 31 / \ / \ / \ / \ 3 5 1 30
Now convert the root, we have to increment left subtree for converting the root.
50 / \ / \ 19 31 / \ / \ / \ / \ 14 5 1 30
Please note the last step – we have incremented 8 to 19, and to fix the subtree we have incremented 3 to 14.
Inorder traversal before conversion is : 3 7 5 50 1 2 30 Inorder traversal after conversion is : 14 19 5 50 1 31 30
Time Complexity: O(n^2), Worst case complexity is for a skewed tree such that nodes are in decreasing order from root to leaf.
Please write comments if you find any bug in the above algorithm or a better way to solve the same problem.