[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] ...
随机推荐
- C++学习(2)—— 数据类型
C++规定在创建一个变量或者常量的时候,必须指定出相应的数据类型,否则无法给变量分配内存 数据类型存在意义:给变量分配合适的内存空间 1. 整型 作用:整型变量表示的是整数类型的数据 C++中能够表示 ...
- uiautomator2+python自动化测试2-查看app页面元素利器weditor
前言 android sdk里面自带的uiautomatorviewer.bat可以查看手机app上的元素,但是不太好用,网上找了个大牛写的weditor,试用了下还是蛮不错的 python环境:3. ...
- Python常用标准库函数
math库: >>> import math >>> dir(math) ['__doc__', '__loader__', '__name__', '__pack ...
- 使用mybatis框架实现带条件查询-单条件
之前我们写的查询sql都是没有带条件的,现在来实现一个新的需求,根据输入的字符串,模糊查询用户表中的信息 UserMapper.xml UserMapper.java 与jdbc的比较: 编写测试方法 ...
- mov offset和lea的区别
mov offset和lea的区别 原文地址:https://www.cnblogs.com/fanzi2009/archive/2011/11/29/2267725.html 全局变量取地址用mo ...
- 开源项目 03 DocX
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- go的接口内部实现
1 前言 1.1 Go汇编 Go语言被定义为一门系统编程语言,与C语言一样通过编译器生成可直接运行的二进制文件.这一点与Java,PHP,Python等编程语言存在很大的不同,这些语言都是运行在基于C ...
- JavaScript操作BOM
window对象的属性: history: 方法: back() 加载 history 对象列表中的前一个URL forward() 加载 history 对象列表中的下一个URL go() 加载 h ...
- Cobaltstrike与Metasploit会话转换
这里只做记录,不做详解 0x00 实验环境 被控制机:192.168.126.129 Metasploit:192.168.126.128 Cobaltstrike:182...* 0x01 CS会话 ...
- 【洛谷】P4198 楼房重建(线段树)
传送门 分析 被线段树按在地上摩擦 先把左边转化成斜率,那么这个题就转化成每次修改一个点的值,输出前缀最大值的个数 看到标签是线段树,所以还是想想线段树的做法吧 既然是线段树,那么就要将区间分成两半 ...