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.
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int depth(TreeNode *node){
if(node==NULL)
return ;
int i=depth(node->left);
int j=depth(node->right);
return max(i,j)+;
}
bool isBalanced(TreeNode *root) {
if(root==NULL)
return true;
int leftDep=depth(root->left);
int rightDep=depth(root->right);
if(abs(leftDep-rightDep)<=)
return isBalanced(root->left) && isBalanced(root->right);
else
return false;
}
};
Balanced Binary Tree 判断平衡二叉树的更多相关文章
- 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 (平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【easy】110. Balanced Binary Tree判断二叉树是否平衡
判断二叉树是否平衡 a height-balanced binary tree is defined as a binary tree in which the depth of the two su ...
- LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)
翻译 给定一个二叉树,决定它是否是高度平衡的. (高度是名词不是形容词-- 对于这个问题.一个高度平衡二叉树被定义为: 这棵树的每一个节点的两个子树的深度差不能超过1. 原文 Given a bina ...
- LeetCode OJ:Balanced Binary Tree(平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Balanced Binary Tree(平衡二叉树)
来源:https://leetcode.com/problems/balanced-binary-tree Given a binary tree, determine if it is height ...
- LeetCode Balanced Binary Tree (判断平衡树)
题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1. 思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决. / ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
随机推荐
- (视频分辨率介绍)混淆的概念:SIF与CIF、4CIF与D1
http://www.microjie.com/index.php/professional-knowledge/82-standards-parterns/26-profession-knowled ...
- css3动画曲线运动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- redis订阅自动退出
1.打开报错, error_reporting(E_ALL);ini_set('display_errors', '1'); 2.没有报错,不是php最大执行时间问题,原因是socket超时3.有设置 ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- laravel-admin 自定义导出excel功能,并导出图片
https://www.jianshu.com/p/91975f66427d 最近用laravel-admin在做一个小项目,其中用到了excel导出功能. 但是laravel-admin自带的导出功 ...
- IO流7 --- FileWriter写出数据的操作 --- 技术搬运工(尚硅谷)
FileWriter字符输出 @Test public void test3(){ File file = new File("hello1.txt"); FileWriter f ...
- 未压缩的jQuery
/*! * jQuery JavaScript Library v3.4.1 * https://jquery.com/ * * Includes Sizzle.js * https://sizzle ...
- 2018-2-13-wpf-PreviewTextInput-在鼠标输入获得-_u0003
title author date CreateTime categories wpf PreviewTextInput 在鼠标输入获得 � lindexi 2018-2-13 17:23:3 +08 ...
- 洛谷P2903 [USACO08MAR]麻烦的干草打包机The Loathesome Hay Baler
P2903 [USACO08MAR]麻烦的干草打包机The Loathesome Hay Baler 题目描述 Farmer John has purchased the world's most l ...
- golang之结构体
Go 语言使用结构体和结构体成员来描述真实世界的实体和实体对应的各种属性. Go语言通过用自定义的方式形成新的类型,结构体是类型中带有成员的复合类型. Go 语言中的类型可以被实例化,使用new或&a ...