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. 微信小程序button授权页面,用户拒绝后仍可再次授权

    微信小程序授权页面,进入小程序如果没授权跳转到授权页面,授权后跳转到首页,如果用户点拒绝下次进入小程序还是能跳转到授权页面,授权页面如下 app.js  中的 onLaunch或onShow中加如下代 ...

  2. Hadoop YARN学习之监控集群监控Nagios(4)

    doop YARN学习之监控集群监控Nagios(4) 1. Nagios是一个流行的开源监控工具,可以用来监控Hadoop集群. 2. 监控基本的Hadoop服务 调试好脚本后命名为chek_res ...

  3. CAS server 连接mysql的deployerConfigContext.xml配置

    1.deployerConfigContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <b ...

  4. LigerUI用Post\Get\Ajax前后台交互方式的写法

    parms 参数统一 json格式的数据 url 访问后台的url 设置同步参数 [javascript] view plain copy   $.ajaxSetup({ async : false} ...

  5. codeforces_C. Sequence Transformation

    http://codeforces.com/contest/1059/problem/C 题意: 最初给一个1.2.3.…….n的序列,每次操作先将所有元素的最大公约数加入答案序列,然后在序列中任意删 ...

  6. C#readonly 关键字与 const 关键字的区别

    1. const 字段只能在该字段的声明中初始化,readonly 字段可以在声明或构造函数中初始化.因此,根据所使用的构造函数,readonly 字段可能具有不同的值. 2. const 字段是编译 ...

  7. JFinal项目eclipse出现the table mapping of model: com.gexin.model.scenic.Scenic not exists or the ActiveRecordPlugin not start.

    JFinal项目eclipse出现the table mapping of model: com.gexin.model.scenic.Scenic not exists or the ActiveR ...

  8. 初识Python(windows)——下载、安装、使用

    Table of Contents 1. Why is Python 1.1. Python和R 2. python的下载与安装 2.1. python的版本选择 2.2. python的下载 2.3 ...

  9. linux系统查看网络连接情况

    netstat命令状态说明: CLOSED                      没有使用这个套接字[netstat 无法显示closed状态] LISTEN 套接字正在监听连接[调用listen ...

  10. 09-看图理解数据结构与算法系列(B树)

    B树 B树即平衡查找树,一般理解为平衡多路查找树,也称为B-树.B_树.是一种自平衡树状数据结构,能对存储的数据进行O(log n)的时间复杂度进行查找.插入和删除.B树一般较多用在存储系统上,比如数 ...