Skip to content

Instantly share code, notes, and snippets.

@goatslacker
Created November 22, 2011 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goatslacker/1385092 to your computer and use it in GitHub Desktop.
Save goatslacker/1385092 to your computer and use it in GitHub Desktop.
var o = {
value: 1,
left: {
value: 2,
left: {
value: 4
},
right: {
value: 5
}
},
right: {
value: 3,
left: {
value: 6
},
right: {
value: 7
}
}
};
var fn = function (node) {
var arr = [];
var l = node.left;
var r = node.right;
l && arr.push(l.value);
r && arr.push(r.value);
if (l) {
arr = arr.concat(fn(l));
}
if (r) {
arr = arr.concat(fn(r));
}
if (arr.length === 6) {
arr.unshift(node.value);
}
return arr;
};
console.log(fn(o));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment