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,然后再看由根节点的左孩子和右孩子构成的两课树是否都符合条件。

代码:

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
int maxdepth;
public:
void solve(TreeNode *root,int depth){
if(root==NULL)
return;
if(depth>maxdepth)
maxdepth=depth;
solve(root->left,depth+);
solve(root->right,depth+);
}
int getDepth(TreeNode *root) {
maxdepth=;
solve(root,);
return maxdepth;
}
bool isBalanced(TreeNode *root) {
if(root==NULL)
return true;
int l,r;
if(root->left!=NULL){
l=getDepth(root->left);
}else{
l=;
}
if(root->right!=NULL){
r=getDepth(root->right);
}else{
r=;
}
return abs(l-r)<=&&isBalanced(root->left)&&isBalanced(root->right);
}
};

Balanced Binary Tree (二叉树DFS)的更多相关文章

  1. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

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

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

  3. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  4. 110.Balanced Binary Tree Leetcode解题笔记

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

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

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

  6. 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树

    平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...

  7. LeetCode: Balanced Binary Tree 解题报告

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

  8. [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  9. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

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

    110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...

随机推荐

  1. 学习笔记 第九章 使用CSS美化表格

    第9章  使用CSS美化表格 学习重点 正确使用表格标签: 设置表格和单元格属性: 设计表格的CSS样式. 9.1 表格的基本结构 表格由行.列.单元格3部分组成,单元格时行与列交叉的部分. 在HTM ...

  2. 萌新--关于vue.js入门及环境搭建

    十几天闭关修炼,恶补了html跟css以及JavaScript相应的基础知识,恰巧有个群友准备做开源项目,愿意带着我做,但是要求我必须懂vue.js,所以开始恶补vue.js相关的东西. 在淘宝上买了 ...

  3. java中字节和字符的转换操作

    package com.ywx.io; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputSt ...

  4. JVM 优点与缺点的深入分析

    Java 最初诞生的时候,它可以说是其他语言的进化版.不仅因为Java很简单,而且这一进化的语言还是一个可以运行第三方硬件字节码的虚拟机.它还是垃圾收集站,从而令存储管理和内核转储(core dump ...

  5. Swift - 值类型与引用类型的初步探究

    前言 swift中的结构体和类在组成和功能上具有一定的相似性.两者都可以含有成员属性.成员方法用于数据存储和功能性模块封装.往往造成不知如何对二者进行区分和使用 值类型概念和引用类型概念 值类型的概念 ...

  6. Jmeter中的参数化常用的几种方式

    Jmeter中的参数化常用的几种方式,这里讲一下前两个方式,最后一个在csv参数化里已详细讲解. 1.用户参数 2.函数助手 3.CSV Data Set Config  一.用户参数 位置:添加-前 ...

  7. CAD绘制标记(网页版)

    主要用到函数说明: MxDraw::GetCursorPickRect 返回拾取矩形框的宽度,默认值为6.详细说明如下: 参数 说明 IN MXDRAWOCXHANDLE hOcx 控件窗口句柄 OU ...

  8. listener.log文件过大导致oracle假死

    /home/u01/oracle/product/11gr2/db_1/log/diag/tnslsnr/VM_179_95_centos/listener/trace/listener.log li ...

  9. 【C语言】控制台窗口图形界面编程(六):光标设置

    目录 00. 目录 01. CONSOLE_CURSOR_INFO结构 02. GetConsoleCursorInfo函数 03. SetConsoleCursorInfo函数 04. SetCon ...

  10. 动态 SQL(1)

    使用动态 SQL 完成多条件查询 动态 SQL 是 MyBatis 的一个强大的特性.在使用 JDBC 操作数据时,如果查询条件特别多,将条件串联成 SQL 字符串是一件痛苦的事情,通常的解决方法是写 ...