美文网首页
balanced-binary-tree

balanced-binary-tree

作者: DaiMorph | 来源:发表于2019-05-04 11:14 被阅读0次
    class Solution {
    public:
        bool isBalanced(TreeNode *root) {
            return getheight(root)!=-1;
        }
        int getheight(TreeNode*root)
        {
            if(root==NULL)return 0;
            int left=getheight(root->left);
            if(left==-1)return -1;
            int right=getheight(root->right);
            if(right==-1)return -1;
            return abs(left-right)>1?-1:max(left,right)+1;
        }
    };
    

    相关文章

      网友评论

          本文标题:balanced-binary-tree

          本文链接:https://www.haomeiwen.com/subject/zriznqtx.html