题目:

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.

代码:

/**
* 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:
bool isBalanced(TreeNode* root)
{
if (!root) return true;
if ( abs(Solution::height(root->left)-Solution::height(root->right)) <= )
{
return Solution::isBalanced(root->left) && Solution::isBalanced(root->right);
}
else
{
return false;
}
}
static int height(TreeNode* p)
{
if (!p) return ;
return max(Solution::height(p->left),Solution::height(p->right))+;
}
};

tips:

第一次刷这leetcode把此题漏掉了。

注意balanced tree是指every node:

(1)判断每个节点left和right深度是否相等

(2)再判断left和right分别是不是balanced的

【Balanced Binary Tree】cpp的更多相关文章

  1. 【遍历二叉树】10判断二叉树是否平衡【Balanced Binary Tree】

    平衡的二叉树的定义都是递归的定义,所以,用递归来解决问题,还是挺容易的额. 本质上是递归的遍历二叉树. ++++++++++++++++++++++++++++++++++++++++++++++++ ...

  2. 【Invert Binary Tree】cpp

    题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solutio ...

  3. 【Maximum Depth of Binary Tree 】cpp

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  4. 【Minimum Depth of Binary Tree】cpp

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  5. 【Lowest Common Ancestor of a Binary Tree】cpp

    题目: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Accor ...

  6. 【leetcode】Balanced Binary Tree(middle)

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

  7. 【LeetCode】Balanced Binary Tree 算法优化 解题报告

    Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...

  8. 【LeetCode-面试算法经典-Java实现】【114-Flatten Binary Tree to Linked List(二叉树转单链表)】

    [114-Flatten Binary Tree to Linked List(二叉树转单链表)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bin ...

  9. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

随机推荐

  1. 257. Binary Tree Paths (dfs recurive & stack)

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  2. Selenium入门20 等待时间

    自动化过程中有的页面元素加载慢或者需要等待特定条件执行后续步骤,此时需添加等待时间: 1 time.sleep()  固定等待时间,需import time 2 webdriver隐式等待 无需引入包 ...

  3. Cannot start session without errors, please check errors given in your PHP and/or webserver log file and configure your PHP installation properly.错误

    错误如图示: 1.在php的目录下建立个文件夹tmp,这个有权限的问题,如果是ntfs的分区,就一定要添加evryone的控制权限,否则是没用的.2.在php.ini找到session.save_pa ...

  4. C#后台unxi时间戳转换为前台JS时间的方法

    后台返回的时间是一个格式为 /Date(1530153274362)/ 的unxi时间戳前台转换代码:var matchResult = data.match(/(\d+)/);if (matchRe ...

  5. mm struct与pgd

    假如该vm_area_struct描述的是一个文件映射的虚存空间,成员vm_file便指向被映射的文件的file结构,vm_pgoff是该虚存空间起始地址在vm_file文件里面的文件偏移,单位为物理 ...

  6. Codeforces 758B Blown Garland

    题目链接:http://codeforces.com/contest/758/problem/B 题意:一个原先为4色环的链子少了部分,要你找出死的最少的一种可能,各输出四种颜色的死了多少. 分析:就 ...

  7. DevExpress控件经验集合

    关于GridControl的可以先看这里:http://blog.csdn.net/dong413876225/article/details/8313094 增加新行,我用了AddNewRow,但是 ...

  8. caffe resize用interpolation

    opencv的resize默认的是使用双线性插值INTER_LINEAR,也可以是尝试其他的方式进行插值操作 if (param.random_interpolation_method()) { // ...

  9. Java-笔记1

    /* 对第一个java程序进行总结 1. java程序编写-编译-运行的过程 编写:我们将编写的java代码保存在以".java"结尾的源文件中 编译:使用javac.exe命令编 ...

  10. Thinkphp 5 使用DOMDocument

    每一个载入浏览器都会生成一个 Document 对象. Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问.Document 对象是 Window 对象的一部分. 我们项目 ...