LeetCode——Balanced Binary Tree
Description:
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.
判断是不是二叉平衡树,递归还是递归。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null)
return true;
int l = getDepth(root.left, 1);
int r = getDepth(root.right, 1);
if(Math.abs(l-r) > 1) {
return false;
}
else {
return isBalanced(root.left) && isBalanced(root.right);
} }
public int getDepth(TreeNode root, int depth) {
if(root == null) {
return depth;
}
else {
return Math.max(getDepth(root.left, depth+1), getDepth(root.right, depth+1));
} }
}
LeetCode——Balanced Binary Tree的更多相关文章
- 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 TODO
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 binary ...
- LeetCode Balanced Binary Tree (判断平衡树)
题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1. 思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决. / ...
- [LeetCode] Balanced Binary Tree 深度搜索
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
随机推荐
- Android——子线程操作主线程
子线程不能直接操作主线程 UI线程 //水平进度条 public void jdt1_onclick(View view) { final ProgressDialog pd = new Progre ...
- pip下载保存Python包,pip离线安装
新版pip下载安装包命令: pip download -r requirements.txt -d /tmp/paks/ 在linux下 1.下载指定的包到指定文件夹. ...
- java-事务-案例
项目结构: 数据库: /* SQLyog Ultimate v12.09 (64 bit) MySQL - 5.5.53 : Database - threadlocal ************** ...
- HTTP API 设计指南(结尾)
前言 这篇指南介绍描述了 HTTP+JSON API 的一种设计模式,最初摘录整理自 Heroku 平台的 API 设计指引 Heroku 平台 API 指引. 这篇指南除了详细介绍现有的 API 外 ...
- 在网页中使用SVG
SVG可以作为一个独立的文件存在.但更多的时候,我们希望他能集成在某个更大的文档中,比如HTML.将SVG插入到HTML中主要有以下几种方式: 将 SVG 作为图像导入 将 SVG放入 iframe ...
- CSS圆角框,圆角提示框
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- EasyUI 搜索框
1.用法 (1).从标记创建.把 'easyui-searchbox' class 加入到 <input> 标记. <script type="text/javascrip ...
- 安装 Windows SDK for Windows 7 时遇到的一个问题及解决办法
最近试着用 VS2010 + Qt 开发程序,发现 VS2010 里面没有提供单独的调试器 cdb,这样用 Qt Creator 时就无法设置断点调试,很不方便.想起 Windows SDK for ...
- python扩展
补充一些有趣的知识 1. sys模块方法的补充,打印进度条 import sys,time for i in range(20): sys.stdout.write("#") sy ...
- 问题解决-某些项目因位于工作空间目录中而被隐藏 & 如何解决java项目导入出错:与另一项目重叠
有时候我们导入现有的工程时会出现错误,没有继续下一步的那个按钮,错误提示如下:some projects were hidden because they exist in the workspace ...