Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.(Easy)

分析:

利用辅助函数求深度,然后根据heighted-balanced定义判定即可

代码:

 class Solution {
int depth(TreeNode* root) {
if (root == nullptr) {
return ;
}
return max(depth(root -> left), depth(root -> right)) + ;
}
public:
bool isBalanced(TreeNode* root) {
if (root == nullptr) {
return true;
}
int left = depth(root -> left);
int right = depth(root -> right);
if (abs(left - right) <= && isBalanced(root -> left) && isBalanced(root -> right)) {
return true;
}
return false;
}
};

LeetCode110 Balanced Binary Tree的更多相关文章

  1. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  2. 110.Balanced Binary Tree Leetcode解题笔记

    110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...

  3. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  4. 【leetcode】Balanced Binary Tree(middle)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  5. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

  6. [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树

    4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a ...

  7. 平衡二叉树(Balanced Binary Tree)

    平衡二叉树(Balanced Binary Tree)/AVL树:

  8. [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

    Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...

  9. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

随机推荐

  1. Codeforces 899F Letters Removing 线段树/树状数组

    虽然每次给一个区间,但是可以看作在区间内进行数个点操作,同样数列下标是动态变化的,如果我们将每个字符出现看作1,被删除看作0,则通过统计前缀和就能轻松计算出两个端点的位置了!这正是经典的树状数组操作 ...

  2. Django项目:CRM(客户关系管理系统)--51--42PerfectCRM实现AJAX全局账号注册

    #urls.py """PerfectCRM URL Configuration The `urlpatterns` list routes URLs to views. ...

  3. LUOGU P1680 奇怪的分组

    题目背景 终于解出了dm同学的难题,dm同学同意帮v神联络.可dm同学有个习惯,就是联络同学的时候喜欢分组联络,而且分组的方式也很特别,要求第i组的的人数必须大于他指定的个数ci.在dm同学联络的时候 ...

  4. mysql 导入txt数据

    在导入数据的时候,特别是数据包含中文的时候,在导入的时候,容易出现编码规则引起的错误.例如错误提示:Invalid utf8 character string: '' 这种情况下,我们可以把需要导入的 ...

  5. HDU 4584

    //也是简单题,因为n太小,故暴力之! #include<stdio.h> #include<math.h> #include<string.h> #define ...

  6. jsp页面el表达式<c:choose> <c:when的用法

    等于 是if else <c:choose> <c:when test="${paginationModel.py_province != ''}"> 如果 ...

  7. Python各种转义符

    文章来源:https://www.cnblogs.com/luckyplj/p/9792658.html 谢谢作者:雨后观山色

  8. jsp必填项加红色星号

    <th><font color=red>*</font>文字:</th>

  9. JavaScript 对象的所有方法名称转换为大写

    function A() { this.do1 = function () { console.log(1); }; this.do2 = function () { console.log(2); ...

  10. 解决git push、pull时总是需要你输入用户名和密码

    git config --global credential.helper store之后再次执行git push 或者git pull这时候还需要输入用户名和密码 下次就不需要了