题解 | #判断是不是平衡二叉树#
判断是不是平衡二叉树
http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222
class Solution {
public:
bool result = true;
int dfs(TreeNode* pRoot){
if(pRoot == NULL)
return 0;
int left = dfs(pRoot->left);
int right = dfs(pRoot->right);
if(result && abs(left - right) >= 2)
result = false;
return max(left,right) + 1;
}
bool IsBalanced_Solution(TreeNode* pRoot) {
if(pRoot == NULL)
return true;
dfs(pRoot);
return result;
}
};