判断二叉树是平衡二叉树
方法:用递归函数求出每个子树的高度,再判断|高度差|>1时不是平衡二叉树
bool isBalanced(TreeNode* root) { if(root==NULL) return true;//空树是平衡二叉树 //前序遍历式的递归 int left=height(root->left); int right=height(root->right); int k=left-right; if(k<-1||k>1) return false; return isBalanced(root->left)&&isBalanced(root->right); } //求本节点的高度 int height(TreeNode* root){ if(root==NULL) return 0; int l=height(root->left); int r=height(root->right); return l>r? l+1:r+1; }