/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ans = true;
checkHeight(root);
return ans;
}
int checkHeight(TreeNode *root)
{
if(root == NULL) return 0;
int left = checkHeight(root->left);
int right = checkHeight(root->right);
if(abs(left - right) > 1) ans = false;
return max(left, right) + 1;
}
private:
bool ans;
};
[Leetcode]Balanced Binary Tree
最新推荐文章于 2017-06-28 16:04:50 发布