Leetcode 110 Balanced Binary Tree 二叉树

判断一棵树是否是平衡树,即左右子树的深度相差不超过1.

我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int depth(TreeNode* root){
if(!root) return ;
else{
return max(depth(root->left), depth(root->right)) + ;
}
}
bool isBalanced(TreeNode* root) {
if(!root) return true;
else if(abs(depth(root->left) - depth(root->right))> ){
return false;
}
else return isBalanced(root->left) && isBalanced(root->right);
}
};
上一篇:linux下如何查看某个软件 是否安装??? 安装路径在哪???


下一篇:.Net全景视图