leetcode 110 Balanced Binary Tree ----- 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.
判断一棵树是否是平衡二叉树
利用高度的答案,递归求解。
/**
* 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 left = maxDepth(root.left);
int right = maxDepth(root.right); if( left - right > 1 || right - left > 1)
return false;
return isBalanced(root.left)&&isBalanced(root.right); } public int maxDepth(TreeNode root) {
if( root == null )
return 0;
if( root.left == null && root.right == null)
return 1;
if( root.left == null)
return maxDepth(root.right)+1;
if( root.right == null)
return maxDepth(root.left)+1;
return Math.max(maxDepth(root.left),maxDepth(root.right))+1;}
}
leetcode 110 Balanced Binary Tree ----- java的更多相关文章
- 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)的方 ...
- Java for LeetCode 110 Balanced Binary Tree
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 ...
- Java实现LeetCode 110. Balanced Binary Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * Tre ...
- [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 ...
随机推荐
- Python中通过多个字符分割(split)字符串的方法
python中字符串自带的split方法一次只能使用一个字符对字符串进行分割,但是python的正则模块则可以实现多个字符分割 import re re.split('-|_','sharejs_ha ...
- JSP中的EL
1.为什么要使用EL 使用<jsp:getProperty>,只能访问bean属性的性质,不能访问嵌套性质.例如一个含有Dog对象的Person对象. 当然使用脚本可以工作,但是如果不想使 ...
- Rhel6-lanmp架构配置文档
l--操作系统:windows linux unix mac OS a--网页发布软件:apache nginx iis m--数据库:mysql pgsql oracle... p--网页 ...
- 自定义圆的半径layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...
- FZU 2072 - Count
题意:给一个数组,每次查询输出区间内数字x出现的次数. 每次查询数字x是与其它数字无关的,所以我们可以以每个数字为索引建立一个链表,里面存放它出现的下标,这里可以保证是递增的.每次查询数字x,就在x的 ...
- electronic data interchange 电子数据交换
electronic data interchange 电子数据交换
- javaweb-dbcp
package cn.songxinqiang.samples.commonsdbcp.util; import java.sql.Connection;import java.sql.Databas ...
- yii2 利用小部件生成后台左边菜单栏
************ 模型层递归查询权限 ************ /** * 递归方式查询权限 */ public function getPrivilege() ...
- Response响应请求操作
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Te ...
- HDU 3966 (树链剖分+线段树)
Problem Aragorn's Story (HDU 3966) 题目大意 给定一颗树,有点权. 要求支持两种操作,将一条路径上的所有点权值增加或减少ai,询问某点的权值. 解题分析 树链剖分模板 ...