int depth = 0; while (q.size()) { depth ++ ; int sz = q.size(); while (sz -- ) { auto t = q.front(); q.pop(); if (t->left) q.push(t->left); if (t->right) q.push(t->right); } } return depth; }
boolisBalanced(TreeNode* root){ stack<TreeNode*> stk; if (root) stk.push(root); while (stk.size()) { auto t = stk.top(); stk.pop();
if (abs(getDepth(t->left) - getDepth(t->right)) > 1) returnfalse;
if (t->right) stk.push(t->right); if (t->left) stk.push(t->left); } returntrue; } };