как-то так
var f = function(nodes) {
var node,
stack = [];
stack.push(nodes)
while (stack.length) {
node = stack.pop();
if (node.childNodes.length) {
var i = node.childNodes.length;
while(i--) stack.push(node.childNodes[i]);
} else {
console.log(node);
}
}
}