Lintcode93-Balanced Binary Tree-Easy
93. 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.
Example
Example 1:
Input: tree = {1,2,3}
Output: true
Explanation:
This is a balanced binary tree.
1
/ \
2 3
Example 2:
Input: tree = {3,9,20,#,#,15,7}
Output: true
Explanation:
This is a balanced binary tree.
3
/ \
9 20
/ \
15 7
Example 3:
Input: tree = {1,#,2,3,4}
Output: false
Explanation:
This is not a balanced tree.
The height of node 1's right sub-tree is 2 but left sub-tree is 0.
1
\
2
/ \
3 4
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/**
* @param root: The root of binary tree.
* @return: True if this Binary tree is Balanced, or false.
*/
public boolean isBalanced(TreeNode root) {
return maxDepth(root) != -1;
} public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int right = maxDepth(root.right);
int left = maxDepth(root.left);
if (right == -1 || left == -1 || Math.abs(right-left) > 1) {
return -1;
}
return Math.max(right,left) + 1;
}
}
Lintcode93-Balanced Binary Tree-Easy的更多相关文章
- 93. Balanced Binary Tree [easy]
Description Given a binary tree, determine if it is height-balanced. For this problem, a height-bala ...
- [leetcode] 110. Balanced Binary Tree (easy)
原题链接 水题 深度搜索每一节点的左右深度,左右深度差大于1就返回false. class Solution { public: bool isBalanced(TreeNode *root) { b ...
- LeetCode_110. Balanced Binary Tree
110. Balanced Binary Tree Easy Given a binary tree, determine if it is height-balanced. For this pro ...
- [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II
Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...
- 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)的方 ...
- 【LeetCode】Balanced Binary Tree 算法优化 解题报告
Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
随机推荐
- 安装VC6.0安装步骤及心得体会
一.安装步骤: 1.打开网站https://pan.baidu.com/s/nxee/AD ,输入提取密码:wdhk. 2.登录微信账号,将软件下载到D盘. 3.鼠标右键点击VC6.0快捷方式,选择“ ...
- javascript 的引入
目录 一.静态引入 1. html标签script引入 2. esm 中import ModuleName from 'module/path' 3. commonjs 中 const ModuleN ...
- 20190422 8个小时的T-SQL基础视频课件-分享
链接:https://pan.baidu.com/s/1YLjtU0Ymn0rI-KMF0DFehw 提取码:yeuw 我最近缺钱..... SQLSERVER 2016视频T-SQL(一) 路径:h ...
- condition版生产者与消费者模式
1.简介 在爬虫中,生产者与消费者模式是经常用到的.我能想到的比较好的办法是使用redis或者mongodb数据库构造生产者消费者模型.如果直接起线程进行构造生产者消费者模型,线程容易假死,也难以构造 ...
- (转载)Oracle procedure 基本语法
转自:http://www.cnblogs.com/wolfplan/p/4004624.html 关键字: oracle 存储过程 1.基本结构 CREATE OR REPLACE PROCEDUR ...
- FG面经Prepare: BST to Double LinkedList
BST to double linkedlist in inorder traversal sequenceFollow Up: 如果这个BST自带prev, next, 给一个value,插进去这个 ...
- MongoDB基本信息
一.MongoDB简介 来源:在2007年,由纽约一个叫10gen的创业团队开发,公司现在叫做MongoDB Inc,最初被开发为PAAS(平台即服务). 数据库类型:基于分布式文件存储的数据库.由C ...
- Go 初体验 - 错误与异常处理
错误处理是学习任何编程语言都需要考虑的一个重要话题 go 内置的 error 接口是这样的: 先上代码: 输出: 释义: 我们首先定义9行的自定义错误类型 30行再实现 error 接口 34定义打开 ...
- Fiddler抓包【5】_Fiddler过滤
1.User Fiters启用 2.Action Action:Run Filterset now是否运行,Load Filterset加载,Save Filterset保存: 3.Hosts过滤 Z ...
- Vue学习之路---No.3(分享心得,欢迎批评指正)
同样的,我们先来回顾一下昨天学习的内容: 1.利用v-once来组织双向绑定 2.filter{}过滤器的使用(详情请看上一章) 3.computed(计算属性),利用computed属性实现filt ...