Skip to content

Instantly share code, notes, and snippets.

@brianyu0717
Created July 25, 2016 18:48
Show Gist options
  • Select an option

  • Save brianyu0717/bf5b28d5dbd21be5b493e33c93ab2bd1 to your computer and use it in GitHub Desktop.

Select an option

Save brianyu0717/bf5b28d5dbd21be5b493e33c93ab2bd1 to your computer and use it in GitHub Desktop.
Programming challenge
Given an array of strings representing a tree. Translate this to a tree.
Example.
["1", "1.1", "1.1.1", "1.2", "1.3", "1.3.1", "2", "2.1"]
Would result in the below tree:
{
key: 'root',
children: [{
key: '1',
children: [{
key: '1.1',
children: [{
key: '1.1.1',
children:[]
}]
}, {
key: '1.2',
children: []
}, {
key: '1.3',
children: [{
key: '1.3.1',
children:[]
}]
}]
}, {
key: '2',
children: [{
key: '2.1',
children:[]
}]
}]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment