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 ...
随机推荐
- 学号 20175201张驰 《Java程序设计》第6周学习总结
学号 20175201张驰 <Java程序设计>第6周学习总结 教材学习内容总结 第7章 ·1.Java支持在一个类中声明另一个类,这样的类称作内部类,而包含内部类的类称为内部类的外嵌类 ...
- 微信小程序 加载图片时,先拉长,再恢复正常
今天在写小程序,发现小程序的图片image如过mode设置为widthFix的话, 加载图片会被先拉伸,后恢复正常 我的处理方法是,给他一个初始的height值,或者就直接 height:auto
- ESP8266开发笔记
自1999年MIT的Kevin Ash-ton教授首次提出物联网(IoT)的概念至今已经有20年了.放眼现在国内外的物联网市场,真可谓是百家争鸣,方兴未艾,无数家软硬件公司在这里舞浪弄潮,逐鹿其中,上 ...
- vue中遇到的问题:Error: Cannot find module 'chalk'
安装 npm install chalk 如果还缺其他很多模块,那就 npm install 暴力解决问题
- java中,什么是构造函数?什么是构造函数重载?什么是复制构造函数?
当新对象被创建的时候,构造函数会被调用.每一个类都有构造函数.在程序中没有给类提供构造函数的情况下,Java编译器会为这个类创建一个默认的构造函数 Java中构造函数重载和方法重载很相似.可以为一个类 ...
- httpclient方式调用接口
public class ToInterface { /** * post方式提交表单(模拟用户登录请求) */ public static void postForm() { // 创建默认的htt ...
- VMware卸载有残留,再安装时报错提示MSI Failed
引用自吾爱破解论坛:https://www.52pojie.cn/thread-455779-1-1.html 解决方法:软件自动清理法: 软件 地址:下载地址1:链接:http://pan.baid ...
- CF 219D 树形DP
CF 219D [题目链接]CF 219D [题目类型]树形DP &题意: 给一个n节点的有向无环图,要找一个这样的点:该点到其它n-1要逆转的道路最少,(边<u,v>,如果v要到 ...
- zabbix监控实战<3> 之自定义监控实例
第一章 自定义监控tcp状态 命令可以选择ss 或者 netstat ss打印基于socket的统计信息,实际运行下来,ss的速度要比netstat要快得多 1.1 tcp的十一种状态 ...
- 201903<<高效15法则>>
高效15法则,这本书作为时间管理的入门书籍,易读易理解,结构清晰,但是中间的某些篇幅内容过于拖沓....