题目:
输入一棵二叉树,判断该二叉树是否是平衡二叉树
代码:
class Solution {
public:
bool IsBalanced_Solution(TreeNode* pRoot) {
int depth;
return Depth(pRoot, &depth);
}
bool Depth(TreeNode *pRoot, int *pd) {
if (pRoot == nullptr)
return *pd = 0, true;
int left, right;
if (Depth(pRoot->left, &left) && Depth(pRoot->right, &right) &&
abs(left - right) <= 1)
return *pd = max(left, right) + 1, true;
return false;
}
};