Skip to content
Advertisement

Tag: tree

How to convert array into a tree structure in javascript

This is my array. I am just trying to frame a hierarchy tree with grandparent->parent->child name relationship. Please help me fix this. Sample input will be like below Expected Output is something like this. Please ignore if any syntax errors Answer You could use reduce and forEach methods to create nested structure and also one array where you can specify

Build tree from edge pairs and root

I’m trying to write a program that takes an array of edge pairs and turns it into a tree. I’m given a root. In this example, the root is 2. The only constraint is, each node can max have 2 children. Sample input: Expected output: Would look something like this: This is my attempt so far: Feel like this should

Advertisement