110. 平衡二叉树
C代码:DFS
int dfs (struct TreeNode* root) {
if (NULL == root) {
return 0;
}
int leftDepth = dfs(root->left);
int rightDepth = dfs(root->right);
if (fabs(leftDepth - rightDepth) > 1 || leftDepth == -1 || rightDepth == -1) {
return -1;
}
return fmax(leftDepth, rightDepth) + 1;
}
bool isBalanced(struct TreeNode* root){
return dfs(root) >= 0;
}