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= ...
随机推荐
- 微信小程序横向滚动正确姿势
<1>xml文件 <view> <scroll-view scroll-x class="scroll-header"> <view cl ...
- LOJ-6280-数列分块入门4
链接: https://loj.ac/problem/6280 题意: 给出一个长为n 的数列,以及 n个操作,操作涉及区间加法,区间求和. 思路: sum维护区间和, tag维护每个区间多加的,不是 ...
- [51nod 1830] 路径交
问题描述 给定一棵n个点的树,以及m条路径,每次询问第L条到第R条路径的交集部分的长度(如果一条边同时出现在2条路径上,那么它属于路径的交集). 输入格式 第一行一个数n(n<=500,000) ...
- mysql 时间索引执行计划
项目中查询时间断的数据发现查询时间很长.怀疑没有走时间的索引,于是explain一下 EXPLAIN select * from t_order where created_at>'2015-0 ...
- 拉链法解决hashtable冲突问题
拉链法解决冲突.拉链法解决冲突的做法是将所有的相同Hash值的key放在一个链表中,比如key3和key14在hash之后都是0,那么在数组的键为0的地方存储这两个值,形式是链表.如果不能理解我的文字 ...
- MySQL概述 - 一条查询sql语句的执行过程
Server层 连接器 建立连接.获取权限.维持和管理连接. 连接建立比较复杂,建议使用长连接 定期断开长连接 mysql_reset_connection指令 查询缓存 建议关闭,任何更新操作会此t ...
- java-dockerfile
java环境dockefile FROM centos:7 MAINTAINER yon@taexa.com ENV JAVA_HOME /usr/local/jdk ENV JRE_HOME ${J ...
- CUDA开发指南
安装指南:https://blog.csdn.net/qilixuening/article/details/77503631 安装了anaconda不需要安装cuda和cudnn?:https:// ...
- (C#- 多线程) 在线程中创建object,共享问题。
研究如下问题: 1. 在一个进程的主线程中创建一个Object,其他线程都可以访问这个Object,并操作Object的方法. - 多线程同步问题. 2. 在一个进程的多个线程里面,每个线程都创建同一 ...
- c++结构体、共用体和枚举
结构体类型 c++中的结构体成员既可以是数据,也可以是函数 c语言中定义结构体变量必须加struct(这也是很多时候和typedef),但是在c++里面,可以不加 结构体和类的不同在于,结构体中的变量 ...