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 ...
随机推荐
- @Resource、@Autowired等几个注解的区别
1.@Resource注解和@Autowired的区别 @Autowired注解:是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false ...
- 学习python的第一天,python的简单知识
python 是现如今比较火的一种编程语言.在抱着试试的态度我来进行学习下python.要学习python 要先进行环境的安装. 下面是下载链接:https://pan.baidu.com/s/1PW ...
- Windows系统目录
文件功能 编辑 ├—WINDOWS │ ├—system32(存放Windows的系统文件和硬件驱动程序) │ │ ├—config(用户配置信息和密码信息) │ │ │ └—systemprofil ...
- 黑马程序员_毕向东_Java基础视频教程——if 语句(单条语句)(随笔)
if 语句(单条语句) 格式(三种) [注意]:如果 if 控制的语句只有一条,则 这个 { } 括号可以不写 if (条件表达式) { 执行语句; } class Test{ public stat ...
- 系统学习scala--基础
scala基础 安装scala(不推荐使用最新版本,2.11.x够用了) scala官网 2.11.12版本下载页面 这里我选择2.11.12版本,在下载页面往下拉,选择scala-2.11.12.m ...
- 在IntelliJ IDEA中创建和运行java/scala/spark程序
本文将分两部分来介绍如何在IntelliJ IDEA中运行Java/Scala/Spark程序: 基本概念介绍 在IntelliJ IDEA中创建和运行java/scala/spark程序 基本概念介 ...
- JSR303后端校验详细笔记
目录 JSR303 使用步骤 关于不为空 分组校验 自定义校验 完整代码 JSR303 使用步骤 1.依赖 <!--数据校验--> <dependency> <group ...
- 4.3 Go for
4.3 Go for Go的for循环是一个循环控制结构,可以执行循环次数. 语法 package main import "fmt" func main() { //创建方式一, ...
- dockerfile定制docker的脚本文件
Dockerfile文件详解 #指定操作系统 FROM scratch #制作base image 基础镜像,尽量使用官方的image作为base image FROM centos #使用base ...
- 王艳 201771010127《面向对象程序设计(java)》第九周学习总结
实验九 异常.断言与日志 实验时间 2018-10-25 1.实验目的与要求 (1) 掌握java异常处理技术: (2) 了解断言的用法: (3) 了解日志的用途: (4) 掌握程序基础调试技巧: 一 ...