110. Balanced Binary Tree

来源:互联网 发布:魔方数据是什么 编辑:程序博客网 时间:2024/04/28 00:18

二叉平衡树的判断,使用递归的方法

class Solution {public:    bool isBalanced(TreeNode* root) {        return findDepth(root) == -1 ? false : true;    }    int findDepth(TreeNode* root)    {        if(!root) return 0;        int leftDepth = findDepth(root->left);        if(leftDepth == -1) return -1;        int rightDepth = findDepth(root->right);        if(rightDepth == -1) return -1;        if(abs(leftDepth-rightDepth) > 1) return -1;        return max(leftDepth, rightDepth)+1;    }};  
0 0
原创粉丝点击