Created
March 5, 2014 01:16
-
-
Save jfensign/9359315 to your computer and use it in GitHub Desktop.
Unique paths in tree.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tree = { | |
name: "Tree", | |
children: [ | |
{ | |
name: "1", | |
children:[ | |
{ | |
name: "1.1" | |
}, | |
{ | |
name: "1.2", | |
children: [ | |
{ | |
name:"1.2.1" | |
}, | |
{ | |
name:"1.2.2" | |
} | |
] | |
} | |
] | |
}, | |
{ | |
name: "3", | |
children: [ | |
{ | |
name:"3.1" | |
}, | |
{ | |
name:"3.2" | |
} | |
] | |
} | |
] | |
}, | |
recurse_tree = function recurse (root, parent) { | |
var path_prefix = (parent ? (parent+" | ") : "") + root.name; | |
return root.children | |
? [].concat.apply ([], root.children.map (function (v) {return recurse (v, path_prefix); })) | |
: path_prefix; | |
}, | |
example = function () { console.log (recurse_tree (tree)); } (); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment