Distribute nodes of different sizes without overlaps in Node Editor (Python)
Because nodes are not necessarily in the order we want in the node tree, I made a list of the nodes in their proper order (listing them from left to right). I then loop through that list and get the nodes by name in the node tree, moving them accordingly.

import bpy

nodes = bpy.data.materials['Material'].node_tree.nodes
list = ['Texture Coordinate', 'Mapping', 'Checker Texture','Diffuse BSDF', 'Material Output']
loc = 0
for i in list:
    nodes[i].location.x = loc
    loc += nodes[i].width + 50
    nodes[i].location.y = 0


The script is rather simple. I loop through my list of node names; then simply set the node's x location to the loc variable.
The next line loc += nodes[i].width + 50 is adding the width of the node, plus 50 (the margin around the nodes) to the already tallied widths.
Then next time around the next node will get moved over that amount.

The only important part is that you add the width of the node to the loc variable after the node has been moved.