题目:平衡二叉树
1int depth(TreeNode* curr, bool& res)
2{
3 if(curr == nullptr) return 0;
4
5 int left = depth(curr->left, res);
6 int right = depth(curr->right, res);
7
8 if(!res) return 0;
9
10 if(std::abs(left - right) > 1)
11 res = false;
12
13 return std::max(left, right) + 1;
14}
15bool isBalanced(TreeNode* root)
16{
17 bool res = true;
18 depth(root, res);
19 return res;
20}
来源:语雀原笔记