It's always fun to see that someone else came up with (roughly) the same algorithm!

Here's my version:

class Solution { public: bool isBalanced(TreeNode* root) { return work(root, 0) != -1; } int work(TreeNode* node, int d) { if (!node) return d; int a = work(node->left, d+1); int b = work(node->right, d+1); return abs(a - b) <= 1 ? max(a, b) : -1; } };Note the return statement's conditional. (Try working out the possible values for a and b to prove its correctness).