题解 | #判断是不是平衡二叉树#
判断是不是平衡二叉树
https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222
后序遍历,同时计算深度和判断是否为平衡树
class Solution { public: bool IsBalanced_Solution(TreeNode* pRoot) { return depth(pRoot) != -1; } int depth(TreeNode* root) { if(!root) return 0; int l = depth(root->left); if(l == -1) return -1; int r = depth(root->right); if(r == -1) return -1; int sub = abs(l - r); if(sub > 1) return -1; return max(l, r) + 1; } };