If we had each node's subtree sum, our answer would look like this psuedocode: `for each node: ans += abs(node.left.subtreesum - node.right.subtreesum)`

. Let `_sum(node)`

be the node's subtree sum. We can find it by adding the subtree sum of the left child, plus the subtree sum of the right child, plus the node's value. While we are visiting the node (each node is visited exactly once), we might as well do the `ans += abs(left_sum - right_sum)`

part.

```
def findTilt(self, root):
self.ans = 0
def _sum(node):
if not node: return 0
left, right = _sum(node.left), _sum(node.right)
self.ans += abs(left - right)
return node.val + left + right
_sum(root)
return self.ans
```