C++简洁代码(2行):
class Solution {
public:
bool IsBalanced_Solution(TreeNode* pRoot) {
return !pRoot ? true : abs(depth(pRoot->left) - depth(pRoot->right)) <= 1 && IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right);
}
int depth(TreeNode* cur) {//就算二叉树的最大深度
return !cur ? 0 : max(depth(cur->left), depth(cur->right)) + 1;
}
};
京公网安备 11010502036488号