Java for LeetCode 110 Balanced Binary Tree
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.
解题思路:
递归即可,JAVA实现如下:
public boolean isBalanced(TreeNode root) {
if(root==null)
return true;
if(Math.abs(maxDepth(root.left)-maxDepth(root.right))>1)
return false;
return isBalanced(root.left)&&isBalanced(root.right);
}
static public int maxDepth(TreeNode root) {
if(root==null)
return 0;
return Math.max(maxDepth(root.left), maxDepth(root.right))+1;
}
Java for LeetCode 110 Balanced Binary Tree的更多相关文章
- Java实现LeetCode 110. Balanced Binary Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * Tre ...
- 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 ...
随机推荐
- xss---攻击
xss表示Cross Site Scripting(跨站脚本攻击),它与SQL注入攻击类似,SQL注入攻击中以SQL语句作为用户输入,从而达到查询/修改/删除数据的目的,而在xss攻击中,通过插入恶意 ...
- WinRAR4.20注册文件key文件注册码
1.首先安装rar4.2官方版 2.在WinRAR已安装文件夹内新建文本文档,打开文档,把下面代码复制进去 RAR registration datawncnUnlimited Company Lic ...
- MQ学习-RabbitMQ, ActiveMQ, Kafka等
之前学习过RabbitMQ,并且还安装过.安装记录的文章如下: Erlang:http://www.cnblogs.com/charlesblc/p/5512380.html RabbitMQ:htt ...
- vs2012搭建OpenGL环境
1. 下载glut库 glut库地址为:http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip glut全称为:OpenGL ...
- 贴几个spark提交任务的小脚本
spark单个master节点的提交方式 spark-submit --master spark://hadoop-namenode-02:7077 \ --class com.dinpay.bdp. ...
- 微信小程序 - 单个题目
后端传过来的数据,如果通过wx:for遍历出来那就是一个页面全部排下来.... 我的想法就是,页面初始化时设置一个默认值 1/50 就是 index+1 / 50(后端传过来的数组长度),通过控制in ...
- 【Redmine】Redmine 3.0.1 安装与配置
Redmine安装 VM安装Linux 安装Bitnami Redmine 配置环境 1.VM安装Linux 使用虚拟机安装Linux 本文使用的是Centos(CentOS-6.3-x86_64-b ...
- widget 常用UI控件介绍
一.单选框 单选框实例程序: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...
- Maven零散笔记——配置Nexus
安装&配置Nexus 解压后,应该获得如下目录结构: nexus-2.0.6是nexus服务主目录 sonatype-work是真正的仓库,同时包含了nexus的配置,如定时任务.用户配置等 ...
- java与javax有什么区别?
http://zhidao.baidu.com/question/8702158.html java和javax都是Java的API包,java是核心包,javax的x是extension的意思,也就 ...