标签:ret 分享 bool blog png 平衡 src def alt
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param root: The root of binary tree.
* @return: True if this Binary tree is Balanced, or false.
*/
int depth(TreeNode *root) {
if (root == NULL) {
return 0;
}
if (root->left== NULL && root->right == NULL) {
return 1;
}
int lft = depth(root->left);
int rit = depth(root->right);
return max(lft , rit) + 1;
}
bool isBalanced(TreeNode *root) {
if (root == NULL) {
return 1;
}
int lft = depth(root->left);
int rit = depth(root->right);
if (abs(lft-rit) <=1) {
return isBalanced(root->left)&&isBalanced(root->right);
}
else
return 0;
}
};
标签:ret 分享 bool blog png 平衡 src def alt
原文地址:http://www.cnblogs.com/ye-chen/p/7789186.html