Optimize Recursive Function to traverse nested DOM nodes
Note that when you do document.querySelectorAll you are getting not a single node but a list of nodes. This seems to be causing confusion because your function refers to node.length - but a single node does not have any length property. Your original function also does not seem to behave exactly as you want it to in that it excludes the parent node (a in your example).

If you ensure that you are passing the function a single node, then it can indeed be simplified a lot:

function getTree(node) {
    var r = {tag: node.nodeName}, a, i;
    if (node.childElementCount) {
        r.children = [];
        for (i = 0; a = node.children[i]; i++) {
            r.children.push(getTree(a));
        }
    }
    for (i = 0; a = node.attributes[i]; i++) {
        r[a.nodeName] = a.nodeValue;
    }
    return r;        
}
var links = document.querySelectorAll('a');
console.log(getTree(links[0])); // only pass the first node to the function


(JSfiddle here)