[LeetCode] Balanced Binary Tree

int height(TreeNode *root) {
	if(root == NULL) {
		return 0;
	}
	int left_height = height(root->left);
	int right_height = height(root->right);
	return left_height > right_height? (left_height+1) :(right_height+1);
}
bool isBalanced(TreeNode *root) {
	if(root == NULL) {
		return true;
	}
	if(isBalanced(root->left) && isBalanced(root->right)
		&& abs(height(root->left)-height(root->right)) <= 1) {
			return true;
	}
	else {
		return false;    
	}
}

你可能感兴趣的:(LeetCode,刷题)