Balanced Binary Tree leetcode java
题目:
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.
题解:
采用递归的方法,要记录depth用来比较。
代码如下:
1 public int checkBalanced(TreeNode t){
2 if(t==null)
3 return 0;
4
5 int leftheight = checkBalanced(t.left);
6 if(leftheight == -1)
7 return -1;
8
9 int rightheight = checkBalanced(t.right);
if(rightheight == -1)
return -1;
if(Math.abs(leftheight-rightheight)>1)
return -1;
else
return Math.max(leftheight,rightheight)+1;
}
public boolean isBalanced(TreeNode root) {
if(checkBalanced(root) == -1)
return false;
else
return true;
}
Balanced Binary Tree leetcode java的更多相关文章
- 110. Balanced Binary Tree - LeetCode
Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- Balanced Binary Tree(Java代码没有结束,是什么原因???)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Balanced Binary Tree [LeetCode]
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Balanced Binary Tree——LeetCode
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Minimum Depth of Binary Tree leetcode java
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- Maximum Depth of Binary Tree leetcode java
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- LeetCode算法题-Balanced Binary Tree(Java实现)
这是悦乐书的第167次更新,第169篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第26题(顺位题号是110).给定二叉树,判断它是否是高度平衡的.对于此问题,高度平衡二 ...
- Balanced Binary Tree --Leetcode C++
递归 左子树是否为平衡二叉树 右子树是否为平衡二叉树 左右子树高度差是否大于1,大于1,返回false 否则判断左右子树 最简单的理解方法就是如下的思路: class Solution { publi ...
随机推荐
- windows下mysql配置(第一次)
忙活了大半天,总算配置好了,本文献给windows下没试用过Mysql的小白,勿喷 http://blog.csdn.net/z1074907546/article/details/51482718 ...
- [HDU - 5408] CRB and Farm
题意: 给出一个由n个点组成的凸包,以及凸包内的k个点,问是否能够在凸包上选择最多2k个点构造一个新的 凸包,使得新的凸包覆盖原来的k个点. 要求2k个点覆盖原本的k个点,只要对原k个点构造凸包,然后 ...
- Python并发编程系列之协程
1 引言 协程是近几年并发编程的一个热门话题,与Python多进程.多线程相比,协程在很多方面优势明显.本文从协程的定义和意义出发,结合asyncio模块详细讲述协程的使用. 2 协程的意义 2.1 ...
- 浅谈Comparable与Comparator的区别
平时进行自定义排序一直使用实现Comparable接口,一段时间后操作的时候居然发现有了个Comparator接口 上网差了些资料,总结笔记一下. 基本原理就是比较,底层是二叉树 比如是3,6,5,1 ...
- 深入理解ajax系列第七篇
前面的话 虽然ajax全称是asynchronous javascript and XML.但目前使用ajax技术时,传递JSON已经成为事实上的标准.因为相较于XML而言,JSON简单且方便.本文将 ...
- django 启动nginx的时候,报端口被占用的问题
Nginx [emerg] listen() to 0.0.0.0:80, backlog 511 failed (98: Address already in use) Nginx更新后经常遇到这样 ...
- redis 客户端命令
Redis 通过监听一个 TCP 端口或者 Unix socket 的方式来接收来自客户端的连接 1 .CLIENT LIST 返回连接到 redis 服务的客户端列表 2 .CLIENT SETNA ...
- 在Windows上安装FFmpeg程序
原文地址:http://helloway.blog.51cto.com/7666282/1642247 FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.它提供了录 ...
- luoguP2231 [HNOI2002]跳蚤
题目链接 bzoj1220: [HNOI2002]跳蚤 题解 根据裴蜀定理,不定方程的解为未知数的gcd,所以选取的n个数的gcd为1 那么n - 1个数保证没有公约数为m的约数,枚举质因数容斥 质因 ...
- [LOJ2541][PKUWC2018]猎人杀(容斥+分治+FFT)
https://blog.csdn.net/Maxwei_wzj/article/details/80714129 n个二项式相乘可以用分治+FFT的方法,使用空间回收可以只开log个数组. #inc ...