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

看板Marginalman作者 (彗星雷達)時間2年前 (2023/03/19 15:25), 編輯推噓5(500)
留言5則, 5人參與, 2年前最新討論串12/14 (看更多)
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
文章代碼(AID): #1a5hZjYO (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1a5hZjYO (Marginalman)