题解 | #判断是不是平衡二叉树#
判断是不是平衡二叉树
https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222
class Solution {
public:
map<TreeNode *,int> hs;
int depth(TreeNode *root){
if(!root)return 0;
if(hs.find(root)!=hs.end())return hs[root];
int ldep=depth(root->left);
int rdep=depth(root->right);
return hs[root]=max(ldep,rdep)+1;
}
bool jude(TreeNode *root){
if(!root) return true;
return abs(hs[root->left]-hs[root->right])<=1&&jude(root->left)&&jude(root->right);
}
bool IsBalanced_Solution(TreeNode* pRoot) {
depth(pRoot);
return jude(pRoot);
}
};

