题解 | #判断是不是平衡二叉树#
判断是不是平衡二叉树
https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222
class Solution { public: int Depth(TreeNode* Root) {//先求二叉树的最大深度,前面有道题就是该思路; if (!Root) return 0;//考虑特殊情况 return max(Depth(Root->left), Depth(Root->right)) + 1; } bool IsBalanced_Solution(TreeNode* pRoot) { if (!pRoot) return true;//考虑特殊情况 if ((abs(Depth(pRoot->left) - Depth(pRoot->right)) <= 1) &&//如果满足平衡二叉树的条件 IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right)) return true; else return false; } };