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 平衡二叉树的更多相关文章

  1. LeetCode 110. Balanced Binary Tree平衡二叉树 (C++)

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  2. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  3. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

  4. LeetCode 110. Balanced Binary Tree (平衡二叉树)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  5. LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)

    翻译 给定一个二叉树,决定它是否是高度平衡的. (高度是名词不是形容词-- 对于这个问题.一个高度平衡二叉树被定义为: 这棵树的每一个节点的两个子树的深度差不能超过1. 原文 Given a bina ...

  6. LeetCode 110. Balanced Binary Tree(判断平衡二叉树)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  7. 【LeetCode】Balanced Binary Tree(平衡二叉树)

    这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...

  8. LeetCode之Balanced Binary Tree 平衡二叉树

    判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...

  9. 110 Balanced Binary Tree 平衡二叉树

    给定一个二叉树,确定它是高度平衡的.对于这个问题,一棵高度平衡二叉树的定义是:一棵二叉树中每个节点的两个子树的深度相差不会超过 1.案例 1:给出二叉树 [3,9,20,null,null,15,7] ...

随机推荐

  1. G1垃圾收集器系统化说明【官方解读】

    还是继续G1官网解读,上一次已经将这三节的东东读完了,如下: 所以接一来则继续往下读: Reviewing Generational GC and CMS[回顾一下CMS收集器] The Concur ...

  2. 《exception》第九次团队作业:Beta冲刺与验收准备(第三天)

    一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 1.掌握软件黑盒测试技术:2.学会编制软件项目 ...

  3. Python应用之-file 方法

    #!/usr/bin/env python # *_* coding=utf-8 *_* """ desc: 文件方法 ######################### ...

  4. Python库资源大全【收藏】

    本文是一个精心设计的Python框架.库.软件和资源列表,是一个Awesome XXX系列的资源整理,由BigQuant整理加工而成,欢迎扩散.欢迎补充! 对机器学习.深度学习在量化投资中应用感兴趣的 ...

  5. python SQLAlchemy的简单配置和查询

    背景: 今天小鱼从0开始配置了下 SQLAlchemy 的连接方式,并查询到了结果,记录下来 需要操作四个地方 1. config  ------数据库地址 2.init ----- 数据库初始化 3 ...

  6. 软帝学院教你java命名规范法则

    java命名规范法则大全 在我们在刚开始学习java的时候,给包.类.方法等命名的时候总是取名不规范,大多都是随便取的,对于一个专业的程序员来说.命名规范化也是必不可少的.命名规范的话能够在编码过程中 ...

  7. php导出表格两种方法 ——PhpExcel的列子

    php常用的导出表格有两种方法,第一种是输出表格,这种方法打开的时候有警告提示,一般导出表格会用phpexcel,这个导出比较灵活,而且还可以设置表格的样式. 第一种导出例子 /** * 执行导出 * ...

  8. Java 第十次作业

    题目1:计算通过中介买房的过程,需交纳的中介费和契税. 代码 /** Business接口中: 两个成员变量RATIO,TAX分别代表房屋中介收取的中介费用占房屋标价的比例及购房需要交纳的契税费用占房 ...

  9. JSP九大隐式对象和四大域对象-----面试

    因为jsp实质是一个Servlet对象:jsp在第一次访问时会被Web容器翻译成Servlet,在执行过程:第一次访问---->inex.jsp---->index_jsp.java--- ...

  10. 开源项目 10 CSV

    using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Data; using Syst ...