题目:

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. April 24 2017 Week 17 Monday

    Much effort, much prosperity. 越努力,越幸运. I have ever seen this sentence in many people's signature of ...

  2. CRUD全栈式编程架构总结

    这里放出实例代码 github.com/SkyvenXiong/HCC

  3. IOS OAuth授权分析

    一.黑马微博 ---> 用户的微博数据1.成为新浪的开发者(加入新浪微博的开发阵营)* 注册一个微博帐号,登录http://open.weibo.com帐号:643055866@qq.com密码 ...

  4. redis 系列 在 vs上 set,get 键值

    1.启动两个 cmd,一个用于打开服务,一个用于运行客户端. 详细步骤可见上一篇文章 2.下载nuget的 ServiceStack.Redis;  ,并在using中引用 ,详细步骤可见上一篇文章 ...

  5. Ubuntu下几种常用的文本编辑器

    常见的基于控制台的文本编辑器有以下几种: emacs           综合性的GNU emacs 编辑环境 nano              一个类似于经典的pico的文本编辑器,内置了一个pi ...

  6. 使用nssm将bat文件注册为windows service (eg:solr, nodejs)

    nssm下载:http://pan.baidu.com/s/1sjAEevj _install.bat @echo off Set BasePath=D:\Tools %BasePath%\nssm- ...

  7. orale 10g和11g中的自动统计任务

    orale 10g和11g中的自动统计任务 博客分类:  数据库相关/oracle   1)  先来看下oracle 10g中的自动统计任务的问题. 从Oracle Database 10g开始,Or ...

  8. webkit几种内核版本的优劣对比总结

    01.Open-Webkit-Sharp 默认不存在JS对话框,如果需要显示alert box,则需要在ShowJavaScriptAlertPanel进行捕捉,然后显示对话框: 02.Open-We ...

  9. ES6笔记03-Set和Map数据结构

    ES6提供了新的数据结构Set.它类似于数组,但是成员的值都是唯一的,没有重复的值.Set本身是一个构造函数,用来生成Set数据结构. var s = new Set(); [2, 3, 5, 4, ...

  10. lvs+ipvsadm负载均衡

    使用LVS实现负载均衡原理及安装配置详解 负载均衡集群是 load balance 集群的简写,翻译成中文就是负载均衡集群.常用的负载均衡开源软件有nginx.lvs.haproxy,商业的硬件负载均 ...