4 ms·
Wow... you got... erm. something very different than I did. function mapTree(tree, fun) { // apply function to tree.value, then recurse (needs javascript
by gcr 16y ago
Wow... you got... erm. something very different than I did.
function mapTree(tree, fun) {
// apply function to tree.value, then recurse (needs javascript.map)
if (typeof tree == 'undefined') {
return null;
}
return {
value: fun(tree.value),
children: tree.children.map(
function(tree) {
return mapTree(tree, fun);
})
};
}
//////////////////
var numTree={value: 42,
children: [
{value: 43, children: []},
{value: 44,
children: [{value: 95, children: []},
{value: 1, children: []}]}
]
};
var count=0;
mapTree(numTree, function(c) {
count+=c;
});
alert(count); // => 225