Balanced Binary Tree(平衡二叉树)
来源:https://leetcode.com/problems/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.
平衡二叉树:是它一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。
Java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private boolean isBalancedFlag = true;
private int getDepth(TreeNode root) {
if(root == null) {
return 0;
}
int leftDepth = getDepth(root.left) + 1;
int rightDepth = getDepth(root.right) + 1;
if(Math.abs(leftDepth - rightDepth) > 1) {
isBalancedFlag = false;
}
return leftDepth > rightDepth ? leftDepth : rightDepth;
}
public boolean isBalanced(TreeNode root) {
getDepth(root);
return isBalancedFlag;
}
}// 1 ms
Python
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
__is_balanced = True
def getDepth(self, pRoot):
if pRoot == None:
return 0
left_depth = self.getDepth(pRoot.left)
right_depth = self.getDepth(pRoot.right)
if abs(left_depth-right_depth) > 1:
self.__is_balanced = False
return left_depth+1 if left_depth>right_depth else right_depth+1
def IsBalanced_Solution(self, pRoot):
self.getDepth(pRoot)
return self.__is_balanced
Balanced Binary Tree(平衡二叉树)的更多相关文章
- [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树
4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a ...
- [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 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode之Balanced Binary Tree 平衡二叉树
判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...
- [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(平衡二叉树)
这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...
- 110 Balanced Binary Tree 平衡二叉树
给定一个二叉树,确定它是高度平衡的.对于这个问题,一棵高度平衡二叉树的定义是:一棵二叉树中每个节点的两个子树的深度相差不会超过 1.案例 1:给出二叉树 [3,9,20,null,null,15,7] ...
- LeetCode 110. Balanced Binary Tree平衡二叉树 (C++)
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- 平衡二叉树(Balanced Binary Tree)
平衡二叉树(Balanced Binary Tree)/AVL树:
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
随机推荐
- JavaScript精进篇
JavaScript是所有前端框架中最基础的框架,在工作了两年以后又重新回到了这里.过去两年里用的最多的前端框架是jquery,因为它简单易上手,而jquery就是封装了JavaScript.重新系统 ...
- gitlab docker中postgresql远程访问配置
1.配置postgresql远程访问 配置postgresql远程访问,需要修改两个文件,在gitlab-ce的docker中位置为 /var/opt/gitlab/postgresql/data 首 ...
- mysql 时间索引执行计划
项目中查询时间断的数据发现查询时间很长.怀疑没有走时间的索引,于是explain一下 EXPLAIN select * from t_order where created_at>'2015-0 ...
- SQL结果统计 GROUP BY
在结果几种,使用GROUP BY进行相同项求和的时候SELECT的字段要与GROUP BY后面的一一对应. select M.TIME,M.PRODUCTMODEL,M.PROCESS_PRODUCT ...
- MySQL 运维管理平台
github: https://github.com/XiaohaoYu/mysql_platform
- linux-ntp-10
Unix/linux类:ntp.aliyun.com,ntp1-7.aliyun.com windows类: time.pool.aliyun.com s1a.time.edu.cn 北京邮电大学 s ...
- z-tree的使用
1.参考资料 1)官网:http://www.treejs.cn/v3/api.php 2)z-tree码云:https://gitee.com/zTree/zTree_v3 2.下载解压 案例演示: ...
- jsp页面实现上传文件,并且还得支持断点续传的功能
我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 首先我们需要了解的是上传文件三要素: 1.表单提交方式:post (get方式提交有大小 ...
- luogu P1352 没有上司的舞会 x
P1352 没有上司的舞会 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员 ...
- SpringCloud 入门知识篇
SpringCloud 入门 springcloud 学习 7天学会springcloud 教程 https://www.cnblogs.com/skyblog/category/738524.htm ...