LeetCode(110):平衡二叉树
Easy!
题目描述:
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
示例 1:
给定二叉树 [3,9,20,null,null,15,7]
3
/ \
9 20
/ \
15 7
返回 true
。
示例 2:
给定二叉树 [1,2,2,3,3,null,null,4,4]
1
/ \
2 2
/ \
3 3
/ \
4 4
返回 false
。
解题思路:
求二叉树是否平衡,根据题目中的定义,高度平衡二叉树是每一个节点的两个字数的深度差不能超过1,那么我们肯定需要一个求各个点深度的函数,然后对于每个节点的两个子树来进行深度差的比较,时间复杂度为O(NlgN)。
C++解法一:
class Solution {
public:
bool isBalanced(TreeNode *root) {
if (!root) return true;
if (abs(getDepth(root->left) - getDepth(root->right)) > ) return false;
return isBalanced(root->left) && isBalanced(root->right);
}
int getDepth(TreeNode *root) {
if (!root) return ;
return + max(getDepth(root->left), getDepth(root->right));
}
};
上面那个方法正确但不是很高效,因为每一个点都会被上面的点计算深度时访问一次,我们可以进行优化。方法是如果我们发现子树不平衡,则不计算具体的深度,而是直接返回-1。那么优化后的方法为:对于每一个节点,我们通过checkDepth方法递归获得左右子树的深度,如果子树是平衡的,则返回真实的深度,若不平衡,直接返回-1,此方法时间复杂度O(N),空间复杂度O(H)。
C++解法二:
class Solution {
public:
bool isBalanced(TreeNode *root) {
if (checkDepth(root) == -) return false;
else return true;
}
int checkDepth(TreeNode *root) {
if (!root) return ;
int left = checkDepth(root->left);
if (left == -) return -;
int right = checkDepth(root->right);
if (right == -) return -;
int diff = abs(left - right);
if (diff > ) return -;
else return + max(left, right);
}
};
LeetCode(110):平衡二叉树的更多相关文章
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
- Java实现 LeetCode 110 平衡二叉树
110. 平衡二叉树 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9 ...
- LeetCode 110.平衡二叉树(C++)
给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9,20,null,nu ...
- [LeetCode 110] - 平衡二叉树 (Balanced Binary Tree)
问题 给出一棵二叉树,判断它是否在高度上是平衡的. 对于本问题,高度上平衡的二叉树定义为:每个节点的两棵子树的深度差永远不大于1的一棵二叉树. 初始思路 根据定义,思路应该比较直接:递归计算每个节点左 ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- LeetCode:平衡二叉树【110】
LeetCode:平衡二叉树[110] 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 ...
- Leetcode:110. 平衡二叉树
Leetcode:110. 平衡二叉树 Leetcode:110. 平衡二叉树 点链接就能看到原题啦~ 关于AVL的判断函数写法,请跳转:平衡二叉树的判断 废话不说直接上代码吧~主要的解析的都在上面的 ...
- [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 (平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 递归 - Leetcode 110 判断二叉树是否为平衡二叉树
110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
随机推荐
- Chain训练准则的计算
轮迭代时验证集的日志: log/compute_prob_valid.1000.log: LOG (nnet3-chain-compute-prob[5.5.100-d66be]:PrintTotal ...
- 数组去重的4种方法(Which one is the fastest???嘻嘻嘻....)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JAVA进阶1
间歇性混吃等死,持续性踌躇满志系列-------------第1天 1.冒泡排序法 import java.util.Arrays; public class SumNum{ public stati ...
- bootstrap-table前端修改后台传来的数据重新进行渲染
使用bootstrap-table显示数据,后台传回数据以后,可能需要对其做调整,如需要前端为数据添加单位 回到顶部 调整数据代码 $("#"+tableId).bootstrap ...
- MySql cmd下的学习笔记 —— 引擎和事务(engine,transaction)
engine 引擎就是MySQL存储数据的不同方式 myisam 插入速度快 支持全文索引 innoDB 插入速度慢 支持事务安全 假设两人同时购买火车票,两人同时看到只有一张火车票,几乎同时下单 或 ...
- nginx使用https协议
效果: nginx添加ssl模块 ./configure --with-http_ssl_module 生成证书 openssl genrsa -out ca.key 2048 openssl req ...
- graph slam BACK END 相关技术资料收集
学习SLAM首推2个网站: 1. WIKI上的SLAM介绍与资源总结:http://en.wikipedia.org/wiki/Simultaneous_localization_and_mappin ...
- FAT文件系统规范v1.03学习笔记---1.保留区之 Fat32 FSInfo扇区结构和备份启动扇区
1.前言 本文主要是对Microsoft Extensible Firmware Initiative FAT32 File System Specification中文翻译版的学习笔记. 每个FAT ...
- 利用微信企业号的告警功能,联动检测ICMP的shell脚本
作者:邓聪聪 由于设备IP众多,为了及时发现IP地址有不可达现象,利用微信的联动报警,及时发现问题,以下是脚本内容!!! ping.sh #!/bin/bash ###SCRIPT_NAME:icmp ...
- 数据库的OLE字段写入长二进制文件
//'*************************************************************************************** //'函数:将数据 ...