/**
* 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
if (root == NULL) {
return true;
}
if (abs(depth(root->left) - depth(root->right)) > 1) {
return false;
}
return isBalanced(root->left) && isBalanced(root->right);
}
private:
int depth(TreeNode *root) {
if (root == NULL) {
return 0;
}
return max(depth(root->left), depth(root->right)) + 1;
}
};
LeetCode Balanced Binary Tree
最新推荐文章于 2017-06-28 16:04:50 发布