这道题给我的教训:别轻视递归。
题干:
代码:
- int geiHeight(TreeNode* node){
- if(node == NULL)return 0;
-
- int leftHeight = getHeight(node -> left);
- if(leftHeight == -1) return -1;
-
- int rightHeight = getHeight(node -> right);
- if(rightHeight == -1) return -1;
-
- if(abs(leftHeight - rightHeight) > 1) return -1;
-
- return 1 + max(leftHeight, rightHeight);
- }
-
- bool isBalanced(TreeNode* root){
- return getHeight(root) == -1 ? false : true;
- }