Re: [LeetCode] 刷到面試 Grind169 C++
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;
}
};
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.137.157.124 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1679210733.A.898.html
推
03/19 15:37,
2年前
, 1F
03/19 15:37, 1F
推
03/19 15:37,
2年前
, 2F
03/19 15:37, 2F
推
03/19 15:38,
2年前
, 3F
03/19 15:38, 3F
推
03/19 15:43,
2年前
, 4F
03/19 15:43, 4F
推
03/19 23:01,
2年前
, 5F
03/19 23:01, 5F
討論串 (同標題文章)
完整討論串 (本文為第 12 之 14 篇):