题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
1 class Solution { 2 public: 3 int TreeDepth(TreeNode* pRoot) 4 { 5 if(pRoot == NULL) 6 return 0; 7 int cnt1 = TreeDepth(pRoot->left); 8 int cnt2 = TreeDepth(pRoot->right); 9 return cnt1 > cnt2 ? cnt1 +1 : cnt2+1; 10 } 11 bool IsBalanced_Solution(TreeNode* pRoot) { 12 if (pRoot == NULL) 13 { 14 return 1; 15 } 16 int cnt1 = TreeDepth(pRoot->left); 17 int cnt2 = TreeDepth(pRoot->right); 18 if (cnt1 - cnt2 > 1 || cnt1 - cnt2 < -1 || !(IsBalanced_Solution(pRoot->left)) || !(IsBalanced_Solution(pRoot->right)) ) 19 { 20 return 0; 21 } 22 return 1; 23 } 24 };