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.

错误的思路(o(N2)时间复杂度):

写一个函数a,用递归遍历的方法,用于计算当前结点的高。

主函数从根节点开始,调用a计算其左子结点和右子结点高差值(×N),如果大于1则返回false,如果小于等于1则继续以左子结点和右子节点分别为根(×N),测试其平衡性;

正确的思路(o(N)时间复杂度):

错误的思路没有正确理解递归。判断平衡二叉树的条件是:①左子树是平衡的;②右子树是平衡的;③左子树和右子树的深度相差不超过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:
bool isBalanced(TreeNode *root) {
int dep = ;
return checkBalance(root, &dep);
} bool checkBalance(TreeNode *node, int *dep) {
if (node == NULL)
return true; int leftDep = ;
int rightDep = ;
if (checkBalance(node->left, &leftDep) &&
checkBalance(node->right, &rightDep) &&
(abs(rightDep - leftDep) <= )) {
*dep = max(leftDep, rightDep) + ;
return true;
} else {
return false;
}
}
};

【Leetcode】【Easy】Balanced Binary Tree的更多相关文章

  1. 【LeetCode】Balanced Binary Tree 算法优化 解题报告

    Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...

  2. 【leetcode】Balanced Binary Tree(middle)

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

  3. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  4. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  5. 【一天一道LeetCode】#104. Maximum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  6. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  7. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  8. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

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

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

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

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

随机推荐

  1. pageHelper 分页插件使用

    第一步:引入pageHelper的jar包. 链接:https://pan.baidu.com/s/1m3EyAmd_eoAsay0aM7uEkA 提取码:xmfi 第二步:需要在SqlMapConf ...

  2. 通过 flume 上传数据到hive

    目标: 通过接受 1084端口的http请求信息, 存储到 hive数据库中, osgi为hive中创建的数据库名称 periodic_report6 为创建的数据表, flume配置如下: a1.s ...

  3. pip安装时的异常,找不到lib2to3\\Grammar.txt

    [From] http://jahu.iteye.com/blog/2353325 异常 : [Errno 2] No such file or directory: 'd:\\python\\pyt ...

  4. 剑指offer——面试题11:快速排序

    #include"iostream" #include"random" using namespace std; /* void Swap(int &a ...

  5. PIE SDK栅格拉伸渲染

    1. 功能简介 栅格数据拉伸渲染是对指定的波段进行图像拉伸,并设置拉伸之后的颜色带,根据像元值和颜色带进行数据渲染. 2. 功能实现说明 2.1. 实现思路及原理说明 第一步 实例化拉伸渲染对象示例 ...

  6. js、css的阻塞问题

    js.css的阻塞问题 这篇文章主要是探索js.css的加载顺序及其影响问题. 下面的代码可以让浏览器阻塞: <!DOCTYPE html> <html lang="en& ...

  7. PHP unlink删除本地中文名称的文件

    由于编码不一样,用unlink()方法删除本地中文名称的材料之前,必须先转码,才能删除成功. 核心代码如下: //删除本地的议题材料(本地上传的材料)             if($local_ma ...

  8. cloudermanager安装过程中出现W:GPG error错误 http://ppa.launchpad.net.trusty Release **** 4DF9B28CA252A784(图文详解)

    不多说,直接上干货! 问题详情  解决办法 sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 4DF9B28CA252A784 ...

  9. Ant利用第三方的task

    转自 http://blog.sina.com.cn/s/blog_3d21e545010006s9.html 一.如何使用第三方任务   Ant可以使用第三方任务,在使用第三方任务之前,需告知Ant ...

  10. Unity3D之OnGUI知识总结

    相对位置参考   http://blog.csdn.net/sunny__chen/article/details/51323265 自适应屏幕收缩  http://www.360doc.com/co ...