Balanced Binary Tree——LeetCode
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,直接返回false。
第二种方式:DFS遍历一遍,遇到左右子树高度差超过1的,记录一个全局变量isBlance为false,最后返回这个boolean的全局变量即可。
显然,第二种方式更优,只需要从根节点计算一遍即可,而第一种方式有很多重复计算。
Talk is cheap>>
第一种方式:
public class BalancedBinaryTree {
public boolean isBalanced(TreeNode root) {
if (root == null)
return true;
List<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode node = queue.get(0);
queue.remove(0);
if (Math.abs(maxDepth(node.left) - maxDepth(node.right)) > 1)
return false;
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
return true;
}
public int maxDepth(TreeNode root) {
if (root == null)
return 0;
int l = maxDepth(root.left);
int r = maxDepth(root.right);
return 1 + Math.max(l, r);
}
}
第二种方式:
private boolean result = true;
public boolean isBalanced(TreeNode root) {
maxDepth(root);
return result;
}
public int maxDepth(TreeNode root) {
if (root == null)
return 0;
int l = maxDepth(root.left);
int r = maxDepth(root.right);
if (Math.abs(l - r) > 1)
result = false;
return 1 + Math.max(l, r);
}
Balanced Binary Tree——LeetCode的更多相关文章
- 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 ...
- Balanced Binary Tree [LeetCode]
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Balanced Binary Tree leetcode java
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- Balanced Binary Tree --Leetcode C++
递归 左子树是否为平衡二叉树 右子树是否为平衡二叉树 左右子树高度差是否大于1,大于1,返回false 否则判断左右子树 最简单的理解方法就是如下的思路: class Solution { publi ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- LeetCode: Balanced Binary Tree 解题报告
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
随机推荐
- Qt 学习之路 :进程间通信
上一章我们了解了有关进程的基本知识.我们将进程理解为相互独立的正在运行的程序.由于二者是相互独立的,就存在交互的可能性,也就是我们所说的进程间通信(Inter-Process Communicatio ...
- cocos2d-x项目过程记录(ios和android设备的适配)
(原创作品,欢迎转载,注明出处,谢谢:http://www.cnblogs.com/binxindoudou/admin/EditPosts.aspx?postid=3213645) 1.原理分析的博 ...
- stl_alloc.h
/* * Copyright (c) 1996-1997 * Silicon Graphics Computer Systems, Inc. * * Permission to use, copy, ...
- Hibernate分页
1. HQL分页: Session session = HibernateUtil.getInstance().getSession(); Query query = session.createQu ...
- meta标签的含义
<meta http-equiv="imagetoolbar" content="false" /> 定是否显示图片工具栏,当为false代表不显示 ...
- mysql查看binlog日志
1.语法:(用于在二进制日志中显示事件.如果您不指定’log_name’,则显示第一个二进制日志.LIMIT子句和SELECT语句具有相同的语法.) show binlog events [IN 'l ...
- webservice调用接口,接口返回数组类型
1. 其中sendSyncMsg1接口是方法名,Vector实现了List接口,xml是sendSyncMsg1的方法形参 Service service = new Service(); Call ...
- Swift - 07 - 布尔类型
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...
- ACE的Socket初步
Tcp通信过程一般为如下步骤: 服务器绑定端口,等待客户端连接. 客户端通过服务器的ip和服务器绑定的端口连接服务器. 服务器和客户端通过网络建立一条数据通路,通过这条数据通路进行数据交互. 常用AP ...
- c#结构体和字节数组的转换、字节数组和stream的转换
本文由博主(YinaPan)原创,转载请注明出处:http://www.cnblogs.com/YinaPan/p/streambytsstruct.html using System; using ...