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 0000000000000000000000000000000000000000..3e19e5e9c124501cfd293e05a5cabea2806190b2 --- /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