/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; * * C语言声明定义全局变量请加上static,防止重复定义 * * C语言声明定义全局变量请加上static,防止重复定义 */ /** * * @param pRoot TreeNode类 * @return bool布尔型 */ int length(struct TreeNode* root){ if(root==NULL){ return 0; } return fmax(length(root->left),length(root->right))+1; } bool IsBalanced_Solution(struct TreeNode* pRoot ) { // write code here if(pRoot==NULL){ return true; } int left_l = length(pRoot->left); int right_l = length(pRoot->right); if(fabs(left_l - right_l) <= 1){ return IsBalanced_Solution(pRoot->left)&&(IsBalanced_Solution(pRoot->right)); }else{ return false; } return true; }