- bool IsBalanced(BinaryTreeNode* pRoot, int* depth)
- {
- if(pRoot== NULL)
- {
- *depth = 0;
- return true;
- }
- int nLeftDepth,nRightDepth;
- bool bLeft= IsBalanced(pRoot->m_pLeft, &nLeftDepth);
- bool bRight = IsBalanced(pRoot->m_pRight, &nRightDepth);
- if (bLeft && bRight)
- {
- int diff = nRightDepth-nLeftDepth;
- if (diff<=1 && diff>=-1)
- {
- *depth = 1+(nLeftDepth > nRightDepth ? nLeftDepth : nRightDepth);
- return true;
- }
- }
- return false;
- }
- bool IsBalanced(BinaryTreeNode* pRoot)
- {
- int depth = 0;
- return IsBalanced(pRoot, &depth);
- }