[LeetCode] 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.
Example 1:
Given the following tree [3,9,20,null,null,15,7]:
3
/ \
9 20
/ \
15 7
Return true.
Example 2:
Given the following tree [1,2,2,3,3,null,null,4,4]:
1
/ \
2 2
/ \
3 3
/ \
4 4
Return false.
给定一个二叉树,判断是否高度平衡。高度平衡二叉树的定义:二叉树的任意节点的两个子树的深度差不超过1。
解法:根据定义,只需要判定一颗二叉树的左右子树高度的高度差是否小于等于1。递归处理每一颗二叉树左右子树的高度,并进行判断再回溯。
Java:
class Solution {
int abs(int x) {
return x > 0 ? x : -x;
}
int check(TreeNode* root) {
if (!root) return NULL;
int lch = check(root -> left);
int rch = check(root -> right);
// 检查子树是否存在不平衡
if (lch == -1 || rch == -1 || abs(lch - rch) > 1) return -1;
// 返回当前子树高度
return (lch > rch ? lch : rch) + 1;
}
public:
bool isBalanced(TreeNode* root) {
return check(root) != -1;
}
};
Java: without ResultType
public class Solution {
public boolean isBalanced(TreeNode root) {
return maxDepth(root) != -1;
}
private int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
if (left == -1 || right == -1 || Math.abs(left-right) > 1) {
return -1;
}
return Math.max(left, right) + 1;
}
}
Python:
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None class Solution:
# @param root, a tree node
# @return a boolean
def isBalanced(self, root):
return (self.getHeight(root) >= 0) def getHeight(self, root):
if root is None:
return 0
left_height, right_height = self.getHeight(root.left), self.getHeight(root.right)
if left_height < 0 or right_height < 0 or abs(left_height - right_height) > 1:
return -1
return max(left_height, right_height) + 1
Python:
class Solution:
"""
@param root: The root of binary tree.
@return: True if this Binary tree is Balanced, or false.
"""
def isBalanced(self, root):
balanced, _ = self.validate(root)
return balanced def validate(self, root):
if root is None:
return True, 0 balanced, leftHeight = self.validate(root.left)
if not balanced:
return False, 0
balanced, rightHeight = self.validate(root.right)
if not balanced:
return False, 0 return abs(leftHeight - rightHeight) <= 1, max(leftHeight, rightHeight) + 1
C++:
class Solution {
public:
bool isBalanced(TreeNode *root) {
if (!root) return true;
if (abs(getDepth(root->left) - getDepth(root->right)) > 1) return false;
return isBalanced(root->left) && isBalanced(root->right);
}
int getDepth(TreeNode *root) {
if (!root) return 0;
return 1 + max(getDepth(root->left), getDepth(root->right));
}
};
C++:
class Solution {
public:
bool isBalanced(TreeNode *root) {
if (checkDepth(root) == -1) return false;
else return true;
}
int checkDepth(TreeNode *root) {
if (!root) return 0;
int left = checkDepth(root->left);
if (left == -1) return -1;
int right = checkDepth(root->right);
if (right == -1) return -1;
int diff = abs(left - right);
if (diff > 1) return -1;
else return 1 + max(left, right);
}
};
C++:
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
int depth(TreeNode *root) {
if (root == NULL) {
return 0;
}
int left = depth(root->left);
int right = depth(root->right);
if (left == -1 || right == -1 || abs(left - right) > 1) {
return -1;
}
return max(left, right) + 1;
} /**
* @param root: The root of binary tree.
* @return: True if this Binary tree is Balanced, or false.
*/
bool isBalanced(TreeNode *root) {
return depth(root) != -1;
}
};
All LeetCode Questions List 题目汇总
[LeetCode] 110. Balanced Binary Tree 平衡二叉树的更多相关文章
- LeetCode 110. Balanced Binary Tree平衡二叉树 (C++)
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- 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(平衡二叉树)(*)
翻译 给定一个二叉树,决定它是否是高度平衡的. (高度是名词不是形容词-- 对于这个问题.一个高度平衡二叉树被定义为: 这棵树的每一个节点的两个子树的深度差不能超过1. 原文 Given a bina ...
- LeetCode 110. Balanced Binary Tree(判断平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【LeetCode】Balanced Binary Tree(平衡二叉树)
这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...
- LeetCode之Balanced Binary Tree 平衡二叉树
判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...
- 110 Balanced Binary Tree 平衡二叉树
给定一个二叉树,确定它是高度平衡的.对于这个问题,一棵高度平衡二叉树的定义是:一棵二叉树中每个节点的两个子树的深度相差不会超过 1.案例 1:给出二叉树 [3,9,20,null,null,15,7] ...
随机推荐
- springboot整合mybatis及封装curd操作-配置文件
1 配置文件 application.properties #server server.port=8090 server.address=127.0.0.1 server.session.tim ...
- 矩阵迹tr(AA*)的计算公式证明
与tr(AB)=tr(BA)的证明思路相同,均使用矩阵的元素表示形式进行证明.
- win10下无法安装loadrunner,提示“管理员已阻止你运行此应用”
如下图: 1.再次进入控制面板,并且选择用户账户后把最下面的[更改用户账户控制设置],里面有个滑条,把滑条拉到最下面的[从不通知]上面并且确定. 2.按[Win+R]快捷键打开运行,输入 gpedit ...
- python笔记37-史上最好用的发邮件zmail
简介 python发邮件之前用的是smtplib,代码太过于复杂,学习成本大,并且很多人学不会.之前专门写过一篇https://www.cnblogs.com/yoyoketang/p/7277259 ...
- 使用gitlab下载代码(附常用命令)
Git是现在很多人常用的代码管理工具,这里有一些常用的命令详解,本人接触也不是很久,若有错误,请在评论指出,谢谢. 若计算机中没有安装GIT,可自行查找安装教程,十分简便. ①首先,我们需要下载项目, ...
- linux中的alias命令详解
功能说明:设置指令的别名.语 法:alias[别名]=[指令名称]参 数 :若不加任何参数,则列出目前所有的别名设置.举 例 :ermao@lost-desktop:~$ alias ...
- 4个MySQL优化工具AWR,帮你准确定位数据库瓶颈!(转载)
对于正在运行的mysql,性能如何,参数设置的是否合理,账号设置的是否存在安全隐患,你是否了然于胸呢? 俗话说工欲善其事,必先利其器,定期对你的MYSQL数据库进行一个体检,是保证数据库安全运行的重要 ...
- 验证符号文件的又一方法(!itoldyouso)
如果您正在开发软件,很可能遇到了“不匹配的PDB”调试器错误.当您将调试器指向错误的符号路径时,通常会发生这种情况. 但有时你确信你所指向的符号是正确的符号,这让你想知道为什么调试器认为这些符号不匹配 ...
- pip包管理工具 基本使用
# 简介 pip是一款包管理工具, 和apt, yum, brew功能类似 # 安装 wget --no-check-certificate https://bootstrap.pypa.io/get ...
- Pytest权威教程(官方教程翻译)
Pytest权威教程01-安装及入门 Pytest权威教程02-Pytest 使用及调用方法 Pytest权威教程03-原有TestSuite的执行方法 Pytest权威教程04-断言的编写和报告 P ...