class Solution {
public:
bool IsBalanced_Solution(TreeNode* pRoot) {
// 判断是否为平衡二叉树
if(!pRoot)
return true;
return dfs(pRoot) == -1?false:true;
}
int dfs(TreeNode* root){
if(!root) return 0;
int left = dfs(root->left);
int right = dfs(root->right);
if(left == -1 || right==-1) return -1;
return abs(left-right)>1?-1:1+max(left,right);
}
};
剑指数据结构-平衡二叉树
最新推荐文章于 2021-07-26 13:34:13 发布