标签:efi shu node abs ret 链接 null turn balance
题目链接:https://leetcode-cn.com/problems/ping-heng-er-cha-shu-lcof/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(!root) return true;
if(abs(getHeight(root -> left) - getHeight(root -> right)) > 1) return false;
return isBalanced(root -> left) && isBalanced(root -> right);
}
bool getHeight(TreeNode* root){
if(!root) return 0;
int lh = getHeight(root -> left);
int rh = getHeight(root -> right);
return lh > rh ? lh + 1 : rh + 1;
}
};
标签:efi shu node abs ret 链接 null turn balance
原文地址:https://www.cnblogs.com/Trevo/p/12683028.html