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= ...
随机推荐
- 【POJ2486】Apple Tree
题目大意:给定一棵 N 个节点的有根树,点有点权,边权均为1.现允许从根节点出发走 K 步,求可以经过的点权之和最大是多少. 题解:可以将点权看作是价值,将可以走的步数看作是重量,则转化成了一个树上背 ...
- oracle partition 分区
--范围分区create table person( id int, name varchar2(20), birth date, sex char(2))partition by range (bi ...
- UVA 11090 : Going in Cycle!! 【spfa】
题目链接 题意及题解参见lrj训练指南 #include<bits/stdc++.h> using namespace std; const double INF=1e18; ; ; in ...
- CF1242C. Sum Balance
题目描述 k组数,每组ni个,数互不相同 把每组数中的一个移到其他组(或者不移动),使得最终每组数的个数不变且总和相等 k<=15,ni<=5000 题解 最终的移动关系一定为若干个环 枚 ...
- 利用aspose-words直接将Word转化为图片
之前遇到一个需求,需要在word文档中加入一些文字,并转化为图片.之前也试过几种方案,但是发现效果还不是很理想,且中间需要经过一次转化为pdf的过程,最近找到了最理想的方式,即利用aspose-wor ...
- List集合中对象的排序
使用到的是: Collections.sort(); 用法是: List<Book> list_book = new ArrayList<Book>(); Book book= ...
- Linux 查看内存(free)、释放内存(基本操作)
原文链接:http://blog.51cto.com/11495268/2384147 1.简介 1.1 介绍 很多时候,服务器 负载 很高(执行操作 很慢),很多 原因 造成 这种 现象(内存不足 ...
- 目标检测:AlexNet
AlexNet是2012年ImageNet竞赛冠军. 它是在CNN的基础上设计的,CNN(卷积神经网络)可谓是现在深度学习领域中大红大紫的网络框架,尤其在计算机视觉领域更是一枝独秀.CNN从90年代的 ...
- 【转】Django restful framework中自动生成API文档
转自 https://www.cnblogs.com/sui776265233/p/11350434.html 自动生成api文档(不管是函数视图还是类视图都能显示) 1.安装rest_framewo ...
- 快速沃尔变换 FWT
P4717 [模板]快速沃尔什变换 #include<bits/stdc++.h> using namespace std; #define int long long #define s ...