剑指offer 039、平衡二叉树
题目
题解
class Solution {
public:
bool IsBalanced_Solution(TreeNode* pRoot) {
if (!pRoot) return true;
return abs(getDepth(pRoot->left) - getDepth(pRoot->right)) <= 1 && IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right);
}
int getDepth(TreeNode* tree) {
if (!tree) return 0;
int left = getDepth(tree->left);
int right = getDepth(tree->right);
return max(left, right) + 1;
}
};