题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
描述
求树的深度,同时判断左右子树的高度。
代码
class Solution {
public:
bool flag;
bool IsBalanced_Solution(TreeNode* pRoot) {
if (pRoot == nullptr) return true;
flag = true;
TreeDepth(pRoot);
return flag;
}
int TreeDepth(TreeNode* root){
if (root == nullptr) return 0;
int l = TreeDepth(root->left);
int r = TreeDepth(root->right);
if (abs(l-r) > 1) flag = false;
return max(l,r)+1;
}
};