题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
class Solution {
public:
bool IsBalanced_Solution(TreeNode* pRoot) {
if(!pRoot) return true;
int C1 = TreeDepth(pRoot->left);
int C2 = TreeDepth(pRoot->right);
if(abs(C1-C2)>1)return false;
bool p1 = IsBalanced_Solution(pRoot->left);
bool p2 = IsBalanced_Solution(pRoot->right);
return p1 && p2;
}
int TreeDepth(TreeNode* pRoot)
{
if (!pRoot)return 0;
int pLeft = TreeDepth(pRoot->left);
int pRight = TreeDepth(pRoot->right);
return max(TreeDepth(pRoot->left)+1,TreeDepth(pRoot->right)+1);
}
};
自底向上算法没弄明白