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. C语言函数中的3个点 ...有什么作用

    标准库提供的一些参数的数目可以有变化的函数.例如我们很熟悉的printf,它需要有一个格式串,还应根据需要为它提供任意多个"其他参数".这种函数被称作"具有变长度参数表的 ...

  2. 微信小程序开发快速入手

    1.在page中的修改数据的setData函数,需要传递的是一个对象. that.setData({ src: res.tempFilePath }) 2.在 onload 事件中,可以获取wx.na ...

  3. c++字符串替换

    #include <string> #include <iostream> using namespace std; string m_replace(string strSr ...

  4. Photoshop之用“色彩范围”命令抠像

    1. 打开一个文件.执行"选择>色彩范围",勾选"本地化颜色族",然后在任务背景上单击取样. 2. 取好样以后点击确定,图片如下所示,执行"选择 ...

  5. pytest进阶使用【fixture(一)fixture与setup/teardown区别】

    fixture翻译为装置. 我觉得名字是很贴合功能的,可以自由给函数装置上自己想要的功能. 当在说pytest比unitest灵活时,fixture肯定是其中的一个理由. 测试数据的准备和执行以后的数 ...

  6. java高级用法之:在JNA中使用类型映射

    目录 简介 类型映射的本质 TypeMapper NativeMapped 总结 简介 JNA中有很多种映射,library的映射,函数的映射还有函数参数和返回值的映射,libary和函数的映射比较简 ...

  7. 前端性能优化之js,css调用优化

    规则1:减少HTTP请求     把多个JS请求合并为一个JS请求,把多个CSS请求合并为一个CSS请求.从而减少从客户端向服务器端的请求数.     规则3:添加Expires头     用http ...

  8. 基于Kubernetes构建企业Jenkins master/slave CI/CD平台

    搭建平台目的: k8s中搭建jenkins master/slave架构,解决单jenkins执行效率低,资源不足等问题(jenkins master 调度任务到 slave上,并发执行任务,提升任务 ...

  9. Windows 下 MSYS2 环境配置和 MinGW-w64 C++ 环境配置

    Windows 下 MSYS2 环境配置和 MinGW-w64 C++ 环境配置 1.简介 本文主要是 Windows 下 MSYS2 环境配置和 MinGW-w64 C++编译环境配置方法 2.下载 ...

  10. 【FAQ】HMS Core推送服务与本地创建通知消息如何相互覆盖?

    我们知道,单独使用HMS Core推送服务或本地创建通知消息,都可以实现通知消息的覆盖,方式分别为: 1.本地创建通知消息(简称本地通知消息) 通过notificationManager.notify ...