LeetCode OJ 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.
做完了链表系列,这几天开始做树系列。对于解决关于树的问题,递归是一个很好的方法。对于这个题目我们也可以用递归来解决,给定一个节点,我们分别判断它的左子树是否平衡,右子树是否平衡。如果它的子树有不平衡的,那么该树是不平衡的。如果它的子树平衡,我们要比较一下两个子树深度差是否超过1。如果超过1,那么该树也是不平衡的。如何记录节点的深度呢,我们可以利用节点中的val来存储一个节点和它的子树组成的树的深度。是不是很巧妙呢?代码如下:
/**
* 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(root.left == null && root.right == null){
root.val = 1;
return true;
} int leftdeep = 0;
int rightdeep = 0;
if(root.left != null){
if(!isBalanced(root.left)) return false;
leftdeep = root.left.val;
}
if(root.right != null){
if(!isBalanced(root.right)) return false;
rightdeep = root.right.val;
} if(Math.abs(leftdeep - rightdeep) > 1) return false;
root.val = leftdeep>rightdeep?leftdeep+1:rightdeep+1;
return true;
}
}
LeetCode OJ 110. Balanced Binary Tree的更多相关文章
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- 【LeetCode OJ】Balanced Binary Tree
Problem Link: http://oj.leetcode.com/problems/balanced-binary-tree/ We use a recursive auxilar funct ...
- 【一天一道LeetCode】#110. Balanced Binary Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】110. Balanced Binary Tree
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- LeetCode OJ:Balanced Binary Tree(平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- 110. Balanced Binary Tree - LeetCode
Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
随机推荐
- Linux修改SSH端口和禁止Root远程登陆
Linux修改ssh端口22 vi /etc/ssh/ssh_config vi /etc/ssh/sshd_config 然后修改为port 8888 以root身份service sshd res ...
- maven创建spring项目之后,启动报错java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoade
错误: org.apache.catalina.core.StandardContext listenerStart严重: Error configuring application listener ...
- Windows下NexusPHP搭建PT站过程
搭建环境:Win7+XAMPP(5.5) 网站根目录: D:\IT\XAMPP5.5\htdocs\ 1 下载源码程序http://sourceforge.net/projects/nexusphp/ ...
- JSON.parse() JSON.stringify() eval() jQuery.parseJSON() 的区别
http://www.jb51.net/article/81880.htm : jQuery.parseJSON(jsonString) : 将格式完好的JSON字符串转为与之对应的Java ...
- Apple pay的使用
Apple pay的使用场景:1.app内:唯品会. 2.线下场景:万达 Apple pay的硬件要求:iphone6 以上 苹果婊 Apple pay的软件要求:国内(应该是)iOS9.2以上 ...
- Spring Security(12)——Remember-Me功能
目录 1.1 概述 1.2 基于简单加密token的方法 1.3 基于持久化token的方法 1.4 Remember-Me相关接口和实现类 1.4.1 Toke ...
- Dokan[转]
原文地址:http://www.cnblogs.com/xxonehjh/p/3634889.html 因工作需要,最近与同事合作使用Dokan开发了一个虚拟磁盘的简单程序,初步实现了远程目录映射到本 ...
- JVM基础(6)-常用参数总结
参考文章: 并发编程网:http://ifeve.com/useful-jvm-flags-part-4-heap-tuning/ 一.参数分类 HotSpot JVM 提供了三类参数. 第一类包括了 ...
- Linux的网卡由eth0变成了eth1,如何修复
Linux的网卡由eth0变成了eth1,如何修复 使用wmware安装了linux,安装成功后,使用的网卡是eth0,没有eth1.但是用过一段时间后,不知道为什么eth0无法使用,系统却自动生 ...
- 监控自定义信息 —— ESFramework 4.0 快速上手(10)
在ESFramework 4.0 进阶(02)-- 核心:消息处理的骨架流程一文中,我们介绍了通过挂接IMessageSpy到骨架流程,我们就可以监控到所有收发的消息.由于Rapid引擎已经为我们组装 ...