Question

110. Balanced Binary Tree

Solution

题目大意:判断一个二叉树是不是平衡二叉树

思路:定义个boolean来记录每个子节点是否平衡

Java实现:

public boolean isBalanced(TreeNode root) {
boolean[] balanced = {true};
height(root, balanced);
return balanced[0];
} private int height(TreeNode node, boolean[] balanced) {
if (node == null) return 0;
int leftHeight = height(node.left, balanced);
int rightHeight = height(node.right, balanced);
balanced[0] = balanced[0] && !(Math.abs(leftHeight - rightHeight) > 1);
return balanced[0] ? Math.max(leftHeight, rightHeight) + 1 : -1;
}

Ref

https://www.youtube.com/watch?v=C75oWiy0bWM

110. Balanced Binary Tree - LeetCode的更多相关文章

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

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

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

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

  3. Leetcode 笔记 110 - Balanced Binary Tree

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

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

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

  5. [LeetCode] 110. Balanced Binary Tree 平衡二叉树

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

  6. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  7. [LeetCode]题解(python):110 Balanced Binary Tree

    题目来源 https://leetcode.com/problems/balanced-binary-tree/ Given a binary tree, determine if it is hei ...

  8. LeetCode 110. Balanced Binary Tree (平衡二叉树)

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

  9. 【一天一道LeetCode】#110. Balanced Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. _CrtCheckMemory

    参考: _CrtCheckMemory MSDN 堆异常检查-MS vs stdio 编写程序经常会涉及到堆的申请,但是如果你向所申请堆里写数据,超过了你最开始申请的空间是,运行中就会发生中断. _C ...

  2. 接口combine

    需求描述 进行复杂项目开发时,服务端(数据接口实现端)会把接口拆分的比较细粒度,以方便在更多地方复用.而拆分后的接口在前端进行组合请求时,通常会出现一个区块要请求5.6个接口甚至更多接口请求才能拿到想 ...

  3. Canvas 核心技术

    最近项目需求中要写较多H5小游戏,游戏本身体量不是很复杂,主要是承载较多业务逻辑,所以决定用canvas来完成游戏部分.之前只是知道H5中有canvas这个东西,也知道它大概是画图的,但具体怎么用,还 ...

  4. error: failed to push some refs to 'git@gitee.com:xxxx'

    出现错误的主要原因是向上仓库的一些文件(README.md,LICENSE等文件)不在本地代码目录中 git pull --rebase origin master 通过这行命令, 可以解决 注释福利 ...

  5. title与h1的区别、b与strong的区别、i与em的区别

    strong标签有语义,是起到加重语气的效果,而b标签是没有的,b标签只是一个简单加粗标签.b标签之间的字符都设为粗体,strong标签加强字符的语气都是通过粗体来实现的,而搜索引擎更侧重strong ...

  6. [ Skill ] map mapc mapcan mapcar mapcon maplist mapinto

    https://www.cnblogs.com/yeungchie/ 几种 map 函数的差异 map map( lambda(( a b ) println( list( a b )) ) list ...

  7. WIN DLL劫持提权

    WIN DLL劫持提权 原理: Windows程序启动的时候需要DLL.如果这些DLL 不存在,则可以通过在应用程序要查找的位置放置恶意DLL来提权.通常,Windows应用程序有其预定义好的搜索DL ...

  8. 前端javascript之BOM、DOM操作、事件

    BOM与DOM操作 BOM 浏览器对象模型>>>:使用js操作浏览器 DOM 文档对象模型>>>:使用js操作前端页面 window对象 所有浏览器都支持 wind ...

  9. vue3 监听路由($route)变化

      setup() {      // ...   },   watch: {     $route(m, n) {       console.log('mm', m)       console. ...

  10. Android四大组件——Activity——Activity之间通信下

    显式意图:一般是用于应用内组件跳转.(如从ActivityA跳转到ActivityB) 隐式意图:一半用于应用之间的跳转.(如从ActivityA跳转到拨号) 隐式意图跳转到百度: 只需将前面Main ...