leetcode 平衡二叉树
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。
示例 1:

输入:root = [3,9,20,null,null,15,7]
输出:true
示例 2:

输入:root = [1,2,2,3,3,null,null,4,4]
输出:false
示例 3:
输入:root = []
输出:true
解题思路
- 检查左子树的高度
- 检查右子树的高度
- 判断左右子树的高度差是否小于等于1
- 如果是,利用递归思想,继续检查左子节点和右子节点是否满足上述条件
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
}
int leftDepth = depth(root.left);
int rightDepth = depth(root.right);
int distance = leftDepth > rightDepth ? leftDepth - rightDepth : rightDepth - leftDepth;
if (distance <= 1) {
return isBalanced(root.left) && isBalanced(root.right);
} else {
return false;
}
}
private int depth(TreeNode node) {
int tdepth = 0;
if (node == null) {
return tdepth;
}
tdepth += 1;
int leftDepth = depth(node.left);
int rightDepth = depth(node.right);
int max = leftDepth > rightDepth ? leftDepth : rightDepth;
return max+tdepth;
}
}
leetcode 平衡二叉树的更多相关文章
- 二叉树的N中遍历方式和拓展应用
(一)创建二叉树,如下图所示,一个标准的二叉树是所有位于根节点的左侧的子节点都是比根节点小的,右侧则都是大于根节点的. public class BinaryNode { public int val ...
- [LeetCode] Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode——Balanced Binary Tree(判断是否平衡二叉树)
问题: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- LeetCode之Balanced Binary Tree 平衡二叉树
判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- [Leetcode] Balanced binary tree平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode:平衡二叉树【110】
LeetCode:平衡二叉树[110] 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 ...
- 【LeetCode】Balanced Binary Tree(平衡二叉树)
这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...
- [LeetCode] 110. Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
随机推荐
- [转帖]How fast are Unix domain sockets?
https://blog.myhro.info/2017/01/how-fast-are-unix-domain-sockets Jan 3, 2017 • Tiago Ilieve Warning: ...
- vCenter 宕机后证书续期处理
vCenter 宕机后证书续期处理 背景 最近护网, 我司被选中作为防守方 因为发现一个vCenter控制台出现异常访问, 被管理员强行断网. 因为是周六的事情,当时自己也没太在意,想着工作日添加网络 ...
- 【转帖】JVM 内存模型与垃圾回收
文章目录 1. JVM内存模型 1.1. 程序计数器 (线程私有) 1.2. Java 虚拟机栈 (线程私有) 1.3. 本地方法栈 (线程私有) 1.4. Java 堆 (线程共享) 1.5. 方法 ...
- Python学习之九_winrm执行远程机器的cmd命令
Python学习之九_winrm执行远程机器的cmd命令 winrm # 注意如下命令需要按照顺序执行. # 打开powershell的管理员模式进行如下的操作. set-executionpolic ...
- 时间片 线程切换 指令周期 流水线 TPS的初步了解
时间片 线程切换 指令周期 流水线 TPS的初步了解 情况说明 Redis 单线程提供服务, 可以支撑十万级别的TPS 通过以个非常简单的测试 redis-benchmark -c 50 -n 500 ...
- [转帖]银河麒麟服务器操作系统V10SP1-x86_64系统环境下部署aarch64虚拟机
文章目录 主机系统环境 搭建aarch64虚拟机环境 ①安装"虚拟系统管理器" ②编译安装Qemu for Aarch64 ③获取aarch64架构的qcow2镜像 ④使用qcow ...
- 使用TFS CI 又想保留服务运行状态的简单方法
最近公司使用TFS-CI的方式定期部署测试环境, 但是发现TFS-CI 运行完之后会清理agent所在的测试环境. 运行的进程都会被killed 1. 第一种方法: 本来第一反应 是使用systemd ...
- How to Use Github
C:\Windows\System32\drivers\etc\hosts 在最后加上一句 20.205.243.166 github.com 从 https://ping.chinaz.com/ 来 ...
- vue中render函数使用attrs绑定id、class、style、事件(5)
1.h函数的三个参数 第一个参数是必须的. 类型:{String | Object | Function} 一个 HTML 标签名.一个组件.一个异步组件.或一个函数式组件. 是要渲染的html标签. ...
- 【JS 逆向百例】W店UA,OB反混淆,抓包替换CORS跨域错误分析
关注微信公众号:K哥爬虫,持续分享爬虫进阶.JS/安卓逆向等技术干货! 声明 本文章中所有内容仅供学习交流,抓包内容.敏感网址.数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后 ...