5 kyu
String tree-ification
140joh_pot
Description:
Write
function word2Tree(str)
that takes in a string str
and converts it into a tree.
Here we convert the string 'great'
into a tree
great
/ \
gr eat
/ \ / \
g r e at
/ \
a t
Every part of the tree needs to be described.
great
would be of type root
gr
would be of type node
g
would be of type leaf
In JSON this tree would look like this:
var ans = {
"value": "great",
"left": {
"value": "gr",
"left": {
"value": "g",
"type": "leaf"
},
"right": {
"value": "r",
"type": "leaf"
},
"type": "node"
},
"right": {
"value": "eat",
"left": {
"value": "e", "type": "leaf"
},
"right": {
"value": "at",
"left": {
"value": "a",
"type": "leaf"
},
"right": {
"value": "t",
"type": "leaf"
},
"type": "node"
},
"type": "node"
},
"type": "root"
};
Please note: str
will always have length > 1.
For splitting, look at the example cases.
Data Structures
Recursion
Fundamentals
Similar Kata:
Stats:
Created | Nov 15, 2016 |
Published | Nov 15, 2016 |
Warriors Trained | 272 |
Total Skips | 25 |
Total Code Submissions | 461 |
Total Times Completed | 140 |
JavaScript Completions | 140 |
Total Stars | 13 |
% of votes with a positive feedback rating | 91% of 55 |
Total "Very Satisfied" Votes | 46 |
Total "Somewhat Satisfied" Votes | 8 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 5 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 6 kyu |