Google India | On-site | New Grad
Anonymous User
1497

Was asked this in second on site interview round in Nov 2021
You have a binary tree. You have to transform the tree such that
1st Constraint
V(parent node) = V(left child) + V(right child)

where, Parent node means a non-leaf node

There are following additional constraints:
2nd Constraint
No nodes value should ever decrease, You can only increment the values of a node or keep the value of the node as is.

3rd thing to optimize: Try to Minimize the total increment made.
T -> Transformed tree
O -> Original tree
[ Sum(all nodes in T) - Sum(all nodes in O) ]

image

struct node {
int value;
node* left;
node* right;
}

Implement => void Transform(node* root)

Comments (6)