leetcode -- Balanced Binary Tree TODO
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 everynode never differ by more than 1.
[解题思路]
检查每个node是否是balanced,大数据集挂掉了
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
if(root == null){
return true;
} return checkBalance(root);
} public boolean checkBalance(TreeNode node){
if(node == null){
return true;
}
int left = getDepth(node.left);
int right = getDepth(node.right);
if(Math.abs(left - right) > 1){
return false;
}
return checkBalance(node.left) && checkBalance(node.right);
} public int getDepth(TreeNode node){
if(node == null){
return 0;
}
int left = getDepth(node.left);
int right = getDepth(node.right);
return Math.max(left, right) + 1;
}
}
leetcode -- Balanced Binary Tree TODO的更多相关文章
- LeetCode: Balanced Binary Tree 解题报告
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- [LeetCode] Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode——Balanced Binary Tree(判断是否平衡二叉树)
问题: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- LeetCode - Balanced Binary Tree
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- [leetcode]Balanced Binary Tree @ Python
原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/ 题意:判断一颗二叉树是否是平衡二叉树. 解题思路:在这道题里,平衡二叉树的定义是二 ...
- [Leetcode] Balanced binary tree平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode Balanced Binary Tree (判断平衡树)
题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1. 思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决. / ...
- LeetCode——Balanced Binary Tree
Description: Given a binary tree, determine if it is height-balanced. For this problem, a height-bal ...
- [LeetCode] Balanced Binary Tree 深度搜索
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
随机推荐
- 无法启动此程序因为计算机中丢失 xxx.dll
“无法启动此程序因为计算机中丢失 XXX.dll” 这类问题在 visual studio 中很常见… 许久不和VS打交道,一碰各种坑… 这是在 VS 2015 Community 出现的问题: (1 ...
- ssh(安全外壳协议)
SSH 为 Secure Shell 的缩写,由 IETF 的网络工作小组(Network Working Group)所制定:SSH 为建立在应用层和传输层基础上的安全协议.SSH 是目前较可靠,专 ...
- XMLDocument转为String 摘录
public static string FormatXmlString(string xmlString) { XmlDocument document = new XmlDocument(); d ...
- Spring Boot构建RESTful API
@Controller:修饰class,用来创建处理http请求的对象 @RestController:Spring4之后加入的注解,原来在@Controller中返回json需要@ResponseB ...
- 微软收购跨平台移动应用开发商Xamarin
微软今天宣布收购移动应用跨平台开发商 Xamarin.收购金额未知.Xamarin 提供了通过 C# 开发 iOS.Android 和 Windows 原生移动应用的工具,以及云端应用測试平台 – 全 ...
- [Jobdu] 题目1139:最大子矩阵
题目描述: 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵.比如,如下4 * 4的矩阵 0 -2 -7 09 2 -6 2-4 1 -4 ...
- 动态修改 dom 元素的伪类样式
最近写代码,需要修改伪类的 content 属性,不想定义两个样式进行切换,而是直接通过 js 进行修改. html 中的伪类(如 a:hover / a:link / class::before / ...
- Debian7/8安装最新的nginx稳定版本
我们知道,通过 apt-get install nginx 就可以安装上nginx,可惜这样安装的nginx版本都有些旧,就连最新的Debian 8.0 默认安装的仍然是1.6.2,更别说 Debia ...
- js prototype 理解
简单理解:prototype对象是实现面向对象的一个重要机制.每个函数也是一个对象,它们对应的类就是 function,每个函数对象都具有一个子对象prototype.Prototype 表示了该函数 ...
- 卖座网一处SQL注射(Http Referer sqlinjection)
漏洞作者: 猪猪侠 漏洞详情 披露状态: 2015-01-13: 细节已通知厂商并且等待厂商处理中2015-01-14: 厂商已经确认,细节仅向厂商公开2015-01-24: 细节向核心白帽子及相关领 ...