今天一个朋友微软电话面试,问到一个问题,就是如题所述的,他问我他写的有没有错,挺难说的,然后我就随手写了一个版本(自我感觉良好)~
bool balance(Node* root, int& height) {
if (root == NULL) {
height = 0;
return true;
}
int left_height = 0, right_height = 0;
bool left_b = balance(root->left, left_height);
bool right_b = balance(root->right, right_height);
height = max(left_height, right_height) + 1;
if (left_height - right_height > 1 || right_height - left_height > 1)
return false;
return left_b && right_b;
}

本文详细介绍了如何解答微软电话面试中关于二叉树平衡性的代码问题,通过给出一个示例代码,解释了如何判断一个二叉树是否平衡,并讨论了代码实现中的关键点。
2157

被折叠的 条评论
为什么被折叠?



