Java实现LeetCode 110. Balanced Binary Tree
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int height(TreeNode root){
if(root == null)
return 0;
int left_height = height(root.left);
int right_height = height(root.right);
return 1 + (left_height > right_height ? left_height : right_height);
}
public boolean isBalanced(TreeNode root) {
if(root == null){
return true;
}
int left_height = height(root.left);
int right_height = height(root.right);
if(Math.abs(left_height - right_height) > 1){
return false;
}
else{
return isBalanced(root.left) && isBalanced(root.right);
}
}
}
Java实现LeetCode 110. Balanced Binary Tree的更多相关文章
- Java for LeetCode 110 Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- leetcode 110 Balanced Binary Tree ----- java
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Java [Leetcode 110]Balanced Binary Tree
题目描述: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced b ...
- [LeetCode] 110. Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode 110. Balanced Binary Tree (平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- Leetcode 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
随机推荐
- 以前的一些word的整理
LAMP部署 环境:虚拟机centos7 安装apache: 命令:yum install -y httpd (在执行这个命令时,可能会遇到运行yum时出现/var/run/yun.pid已被锁定,P ...
- 3.2 Go整数类型
1. Go整数类型 Go语言的数值类型包含不同大小的整数型.浮点数和负数,每种数值类型都有大小范围以及正负符号. 官方文档解释数据类型 int类型中哪些支持负数 有符号(负号):int8 int16 ...
- 最香远程开发解决方案!手把手教你配置VS Code远程开发工具,工作效率提升N倍
文章每周持续更新,原创不易,「三连」让更多人看到是对我最大的肯定.可以微信搜索公众号「 后端技术学堂 」第一时间阅读(一般比博客早更新一到两篇) 今天和大家分享远程开发工具,分享一下我平常是如何用 V ...
- doxygen+graphviz轻松绘制函数调用图(call graph)
前言 之前的工作环境习惯了使用source insight查看函数分析代码,切换到mac下后改用vscode,发现缺少函数调用关系图生成.跨平台的understand可以很好的解决,但是公司没有购买, ...
- MySQL slave状态之Seconds_Behind_Master zz
在MySQL的主从环境中,我们可以通过在slave上执行show slave status来查看slave的一些状态信息,其中有一个比较重要的参数Seconds_Behind_Master.那么你是否 ...
- JS 如何获取自定义属性
<script>var testEle = document.getElementById("test"); testEle.setAttribute("de ...
- 就为了一个原子操作,其他CPU核心罢工了
i++问题 "阿Q赶快回去吧,隔壁二号车间的虎子说我们改了他们的数据,上门来闹事了" 由于老K的突然出现,我不得不提前结束与小黑的交流,赶回了CPU一号车间. 见到我回来,虎子立刻 ...
- netty 实现简单的rpc调用
yls 2020/5/23 netty 实现简单rpc准备 使用netty传输java bean对象,可以使用protobuf,也可以通过json转化 客户端要将调用的接口名称,方法名称,参数列表的类 ...
- COLA的扩展性使用和源码研究
cola扩展点使用和设计初探 封装变化,可灵活应对程序的需求变化. 扩展点使用 步骤: 定义扩展点接口,类型可以是校验器,转换器,实体: 必须以ExtPt结尾,表示一个扩展点. 比如,我定义一个云枢的 ...
- 在win10上搭建pyspark,
最近在研究Spark,准确的说是pyspark,为了搭个测试环境,之前一直在云上开Linux机器来搭Hadoop,但是Spark可以Local执行,我就打算在本地搭个环境.遇到了一些问题,记录一下,也 ...