From b86c6cc32e36688c646a67129099e3feca81e35d Mon Sep 17 00:00:00 2001 From: Powfu <1875065753@qq.com> Date: Tue, 26 Dec 2023 13:04:01 +0000 Subject: [PATCH] =?UTF-8?q?add=202101040022/chapter=5F9/P110=20=E5=B9=B3?= =?UTF-8?q?=E8=A1=A1=E4=BA=8C=E5=8F=89=E6=A0=91.cpp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Powfu <1875065753@qq.com> --- ...1\344\272\214\345\217\211\346\240\221.cpp" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 "2101040022/chapter_9/P110 \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.cpp" diff --git "a/2101040022/chapter_9/P110 \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.cpp" "b/2101040022/chapter_9/P110 \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.cpp" new file mode 100644 index 00000000..3e19e5e9 --- /dev/null +++ "b/2101040022/chapter_9/P110 \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.cpp" @@ -0,0 +1,28 @@ +class Solution { + public: + bool isBalanced(TreeNode* root) { + if (getdepth(root) != -1) { + return true; + } else { + return false; + } + } + int getdepth(TreeNode* node) { + if (node == NULL) { + return 0; + } + int leftlength = getdepth(node->left); + int rightlength = getdepth(node->right); + if (leftlength == -1) { + return -1; + } + if (rightlength == -1) { + return -1; + } + if (abs(leftlength - rightlength) > 1) { + return -1; + } else { + return max(leftlength, rightlength) + 1; + } + } +}; \ No newline at end of file -- Gitee