110. Balanced Binary Tree
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; if(DFS(root)==-1)
return false; return true; } public int DFS(TreeNode root){
int left=1,right=1;
if(root.left!=null)
{
if(DFS(root.left)==-1)
return -1;
else
left=left+DFS(root.left);
}
if(root.right!=null)
{
if(DFS(root.right)==-1)
return -1;
else
right=right+DFS(root.right);
} if(left-right<-1||left-right>1)
return -1; return Math.max(left,right);
} }
110. Balanced Binary Tree的更多相关文章
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- 110. Balanced Binary Tree - LeetCode
Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- [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]题解(python):110 Balanced Binary Tree
题目来源 https://leetcode.com/problems/balanced-binary-tree/ Given a binary tree, determine if it is hei ...
- 【leetcode❤python】110. Balanced Binary Tree
#-*- coding: UTF-8 -*-#平衡二叉树# Definition for a binary tree node.# class TreeNode(object):# def _ ...
随机推荐
- S1:对象与JSON
JSON全称为JavaScript对象表示法(JavaScript Object Notation). JSON是JavaScript中对象的字面量,是对象的表示方法,通过使用JSON,可以减少中间变 ...
- HDU 3074 Multiply game(线段树)
单点更新,更新时先除去 原来的数,因为有去摸,可以用乘上逆元代替. //================================================================ ...
- Linux-Unix版本介绍
转自: http://blog.163.com/li_zhuangs/blog/static/195698098201182411360635/ < DOCTYPE html PUBLIC -W ...
- ctime、atime
Linux系统文件有三个主要的时间属性,分别是ctime(change time, 而不是create time), atime(access time), mtime(modify time).后来 ...
- 经典线程同步 事件Event
阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇 一个经典的多线程同步问题> <秒杀多线程第五篇 经典线程同步关键段CS> 上一篇中使用关键段来解决经典的多线程同步互斥问题 ...
- 【转】CentOS yum安装和卸载软件的使用方法
在CentOS yum安装和卸载软件的使用方法安装方法安装一个软件时. CentOS yum -y install httpd安装多个相类似的软件时 CentOS yum -y install ...
- git 克隆项目 与 分支简单操作
git clone http://abcde.com/myproject/abc.git 克隆远程项目到本地githome文件夹git branch -a 查看所有分支 包括远程和本地 *号开头表示当 ...
- VMware-workstation-full-10.0.1-1379776 CN
从V10版本开始,VMware Workstation 官方自带简体中文了,以后大家不需要汉化啦! 今天,VMware Workstation 10.0.1正式发布,版本号为Build 1379776 ...
- SharePoint 2013 Nintex Workflow 工作流帮助(四)
博客地址 http://blog.csdn.net/foxdave 接上篇点击打开链接 我们来看下一个配置标签Ribbon Option: Task Notification(用于配置分配任务时的提醒 ...
- 开源留言板 --wekan部署
1. 安装ubuntu--server-64位系统 2. 登录ubuntu系统 3. 下载自动安装脚本 #git clone https://github.com/anselal/wekan 4. 执 ...