Re: [LeetCode] 刷到面试 Grind169 C++

楼主: SuiseiLeda (星街雷达)   2023-03-19 15:25:31
110. Balanced Binary Tree easy题
这题有偷看别人怎么写
我好烂
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(!root) return true;
bool balanced = true;
height(root, &balanced);
return balanced;
}
private:
int height(TreeNode* root, bool* balanced){
if(!root) return 0;
int left_height = height(root->left, balanced);
int right_height = height(root->right, balanced);
if(abs(left_height - right_height)>1) {
*balanced = false;
return -1;
}
return max(left_height, right_height) + 1;
}
};
作者: PyTorch (屁眼火炬)   2023-03-19 15:37:00
大师
作者: DreaMaker167 (dreamaker)   2023-03-19 15:37:00
大师
作者: kalama450 (卡拉玛)   2023-03-19 15:38:00
大师
作者: DDFox (冒险者兼清洁工)   2023-03-19 15:43:00
你很棒

Links booklink

Contact Us: admin [ a t ] ucptt.com