LeetCode(24)-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
- 因为二叉树本身是一个递归的结构,所以二叉树的好多问题使可以用递归解决的,考虑root和root.left以及root.right的关系
代码:
/**
* 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(Math.abs(depth(root.left)-depth(root.right)) > 1){
return false;
}
return isBalanced(root.left)&&isBalanced(root.right);
}
public int depth(TreeNode root){
if(root == null){
return 0;
}
return 1+Math.max(depth(root.left),depth(root.right));
}
}
LeetCode(24)-Balanced Binary Tree的更多相关文章
- LeetCode(110) Balanced Binary Tree
题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bina ...
- LeetCode(114) Flatten Binary Tree to Linked List
题目 分析 按要求转换二叉树: 分析转换要求,发现,新的二叉树是按照原二叉树的先序遍历结果构造的单支二叉树(只有右子树). 发现规则,便容易处理了.得到先序遍历,构造即可. AC代码 /** * De ...
- LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal
题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...
- LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal
题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume t ...
- LeetCode(226)Invert Binary Tree
题目 分析 交换二叉树的左右子树. 递归非递归两种方法实现. AC代码 class Solution { public: //递归实现 TreeNode* invertTree(TreeNode* r ...
- [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 ...
- leetcode笔记(二)94. Binary Tree Inorder Traversal
题目描述 (原题目链接) Given a binary tree, return the inorder traversal of its nodes' values. For example:Giv ...
- LeetCode(99) Recover Binary Search Tree
题目 Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chang ...
- LeetCode(98) Validate Binary Search Tree
题目 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined ...
随机推荐
- 剑指Offer——CVTE校招笔试题+知识点总结(Java岗)
剑指Offer(Java岗)--CVTE校招笔试题+知识点总结 2016.9.3 19:00参加CVTE笔试,笔试内容如下: 需要掌握的知识:Linux基本命令.网络协议.数据库.数据结构. 选择题 ...
- iOS7 CookBook精彩瞬间(一)property、selector细节、__unused
1.我们常常使用nonatomic,很多人只知道它的效率较高,却不知道其含义,其含义是非线程安全的,也就是说多线程修改时不加锁,可能出现多个线程先后修改而成为脏数据的情况. 2.unsafe_unre ...
- 【问题汇总】ScrollView嵌套GridView的问题
在开发中遇到了ScrollView嵌套GridView的情况,由于这两款控件都自带滚动条,当它们碰到一起的时候便会出问题,即GridView会显示不全. 解决办法,自定义一个GridView控件. [ ...
- Android 系统当中各种尺寸单位的定义及使用
一,Android 各种标尺单位的含义: px:表示屏幕实际的象素.例如,320*480的屏幕在横向有320个象素,在纵向有480个象素.pt:表示一个点,是屏幕的物理尺寸.大小为1英寸的1/72.i ...
- HTML5中 HTML格式化/HTML样式/链表/表格-样式 韩俊强的博客
HTML5学习从简单到复杂,循环渐进! 每日更新关注:http://weibo.com/hanjunqiang 新浪微博! 1.HTML格式化 <!DOCTYPE html> <h ...
- 学习笔记7-Android短信发送器
新建一个Android项目sns. 在String.xml添加文字 <resources> <stringname="app_name">Sns发送短信&l ...
- Chapter 1 Securing Your Server and Network(13):配置端点安全性
原文出处:http://blog.csdn.net/dba_huangzj/article/details/38489765,专题目录:http://blog.csdn.net/dba_huangzj ...
- c#一些特殊语法
1.using 语法 using不仅可以作为导入包,重命名类名.还可以释放资源 using (Pen gridLinePen = new Pen(Color.red)) { e.Graphics.Dr ...
- Lambda的使用与实战
简介 (下面的简介也可以自己百度,一般进来的都是想知道怎么去用,所以这里主要也是重点在用法与实战上) Lambda表达式是Java SE 8中一个重要的新特性.lambda表达式允许你通过表达式来代替 ...
- 学习pthreads,使用属性对象创建结合线程和分离线程
当我们创建了子线程,是让它犹如脱缰之马,信步驰骋,还是如乖巧听话的孩子,时不时教导一下呢?针对这个问题,本文介绍线程的结合和分离,结构分为三个部分,第一部分给出代码示例,第二部分对代码进行讲解,第三部 ...