1. 程式人生 > >輸入一顆二叉樹,判斷該二叉樹是否是平衡二叉樹

輸入一顆二叉樹,判斷該二叉樹是否是平衡二叉樹

class Solution {
public:
    bool IsBanlanced(TreeNode* pRoot,int* pDepth){
        if(pRoot==NULL){
            *pDepth=0;
            return true;
        }
        int nleft,nright;
        if(IsBanlanced(pRoot->left,&nleft) && 
        IsBanlanced(pRoot->right,&nright)){
            int
diff=nleft-nright; if(diff<=1 && diff>=-1){ *pDepth=1+(nleft>nright?nleft:nright); return true; } } return false; } bool IsBalanced_Solution(TreeNode* pRoot) { int depth=0; return IsBanlanced(pRoot,&depth); } };