/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return bool布尔型 */ int dfs(TreeNode* pRoot, int h) { if(!pRoot) return h; return max(dfs(pRoot->left,h+1),dfs(pRoot->right,h+1)); } bool IsBalanced_Solution(TreeNode* pRoot) { // write code here if(!pRoot) return true; return (abs(dfs(pRoot->left,0)-dfs(pRoot->right,0))<=1) && IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right); } };