From 35bcfc82486333ed07cad3c4c1a8056fe166109d Mon Sep 17 00:00:00 2001 From: Stackingrule <38368554+Stackingrule@users.noreply.github.com> Date: Mon, 21 Dec 2020 16:23:17 +0800 Subject: [PATCH] feat:add Solution.cpp for 0110.Balanced Binary Tree --- .../0110.Balanced Binary Tree/Solution.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 solution/0100-0199/0110.Balanced Binary Tree/Solution.cpp diff --git a/solution/0100-0199/0110.Balanced Binary Tree/Solution.cpp b/solution/0100-0199/0110.Balanced Binary Tree/Solution.cpp new file mode 100644 index 0000000000000..0578b11f2c22f --- /dev/null +++ b/solution/0100-0199/0110.Balanced Binary Tree/Solution.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + bool isBalanced(TreeNode* root) { + if (!root) return true; + if (abs(getDepth(root->left) - getDepth(root->right)) > 1) return false; + return isBalanced(root->left) && isBalanced(root->right); + } + +private: + int getDepth(TreeNode *root) { + if (!root) return 0; + return 1 + max(getDepth(root->left), getDepth(root->right)); + } +}; \ No newline at end of file