平衡二叉树

来源:互联网 发布:原始原素 知乎 编辑:程序博客网 时间:2024/06/07 01:50

题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。

//TreeDepth是前面一道题的函数;IsBalanced_Solution里的return语句,又涉及更早的一道题(树的子结构)class Solution {public:    bool IsBalanced_Solution(TreeNode* pRoot) {        if(!pRoot)            return true;        return IsBalanced_Solution(pRoot->left)&&IsBalanced_Solution(pRoot->right)            &&abs(TreeDepth(pRoot->left)-TreeDepth(pRoot->right))<=1;    }    int TreeDepth(TreeNode* pRoot)    {        if(!pRoot)            return 0;        return max(TreeDepth(pRoot->left),TreeDepth(pRoot->right))+1;    }};
0 0