Let's calculate the depth of a node in the usual way: max(depth of node.left, depth of node.right) + 1. While we do, a path "through" this node uses 1 + (depth of node.left) + (depth of node.right) nodes. Let's search each node and remember the highest number of nodes used in some path. The desired length is 1 minus this number.

```
def diameterOfBinaryTree(self, root):
self.best = 1
def depth(root):
if not root: return 0
ansL = depth(root.left)
ansR = depth(root.right)
self.best = max(self.best, ansL + ansR + 1)
return 1 + max(ansL, ansR)
depth(root)
return self.best - 1
```