At DoorDash, menus are updated daily even hourly to keep them up-to-date. Each menu can be regarded as a tree. When the merchant sends us the latest menu, can we calculate
how many nodes have changed/added/deleted?
Assume each Node structure is as below:
class Node {
String key;
int value;
List children;
}
Assume there are no duplicate nodes with the same key.
Output: Return the number of changed nodes in the tree.
Existing tree
a(1)
/
b(2) c(3)
/ \
d(4) e(5) f(6)
New tree
a(1)
c(3)
f(66)
a(1) a is the key, 1 is the value
For example, there are a total of 4 changed nodes. Node b, Node d, Node e are automatically set to inactive. The value of Node f changed as well.
Existing tree
a(1)
/
b(2) c(3)
/ \
d(4) e(5) g(7)
New tree
a(1)
/
b(2) h(8)
/ | \
e(5) d(4) f(6) g(7)
There are a total of 5 changed nodes. Node f is a newly-added node. c(3) and old g(7) are deactivated and h(8) and g(7) are newly added nodes
followup print out the changes