剪枝改进一下,不需要返回-1,直接使用isBalanced即可 class Solution { public: bool isBalanced=true; bool IsBalanced_Solution(TreeNode* pRoot) { depth(pRoot); return isBalanced; } int depth(TreeNode*p){ if (!p) return 0; if (!isBalanced) return -1; int l = depth(p->left); int r = depth(p->right); if (abs(l-r)>1) isBalanced = false; return max(l,r)+1; } };
点赞

相关推荐

不愿透露姓名的神秘牛友
2025-12-13 09:17
拼多多 算法 43x18 硕士985
理性的熊熊在努力存钱:这还不高吗
点赞 评论 收藏
分享
优秀的大熊猫在okr...:多益:此贼,必有同谋,按律,该当连坐!
你不能接受的企业文化有哪...
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务