Construct a N-nary tree based on root to leaf path
Anonymous User
532

I had an interview with a FAANG company yesterday and had a problem I've never seen. Need urgent help!

Given an array of root to leaf path of a n-nary tree, reconstruct the tree using this relationship.
For example, if you are given

["root", "food"]
["root", "food",  "chips"]
["root", "food", "chips", "doritos"]
["root", "drink", "soda"]
["root", "drink", "soda", "diet coke"]

You should construct a n-nary tree data structure something like this

								root
							/               \
					food                 drink
				/                               |
				chips                    soda
				|                                |
				doritos                   diet coke

Note this is a nary tree not a binary tree. Looking forward to help. The basic data structure of each node is like this

class Node {
	String name;
	List<Node> children;
}
Comments (3)