题目
1 class Solution { 2 public: 3 bool isBalanced(TreeNode* root) { 4 if(root == NULL) return true; 5 return abs(height(root->left)-height(root->right)) <= 1 && isBalanced(root->left) && 6 isBalanced(root->right); 7 } 8 int height(TreeNode* root){ 9 if(root == NULL) return 0; 10 return max(height(root->left),height(root->right)) + 1; 11 } 12 };
一开始不会对每个结点进行高度判断,判断平衡的递归思想和镜像对称二叉树的那个题一个套路