LeetCode110. Balanced Binary Tree

来源:互联网 发布:使命召唤8武器数据 编辑:程序博客网 时间:2024/06/16 07:22

题目链接

判断是否为平衡二叉树。

class Solution {public:    bool isBalanced(TreeNode* root) {        if(root == NULL) return 1;        int l1 = getDepth(root->left);        int l2 = getDepth(root->right);        if(abs(l1-l2)>1) return 0;        return isBalanced(root->left)&&isBalanced(root->right);    }    int getDepth(TreeNode* root){        if(root == NULL) return 0;        return max(getDepth(root->left), getDepth(root->right))+1;    }};
0 0
原创粉丝点击