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. POJ - 1011 剪枝练习

    题意:给定n条拆掉的棍子,问能凑成最短的多条相同长度棍子的最短长度 x:当前第几条正在合成的棍子 y:目前正在尝试的拆掉的棍子 z:当前长度 剪枝方案: 1.按照长度单调性排序,减少重复搜索 2.如果 ...

  2. java中的线程(2):如何正确停止线程之3种常见停止方式

    1.常见停止方式 自定义线程,其中含退出标志位,在run中判断它. 使用interrupt()方法中断线程 使用stop方法暴力终止(已经弃用) 2.使用标志位 class TestThread ex ...

  3. 从零开始学Linux(11)--more

    more命令,是将文件的内容从上到下显示在屏幕上. more会以一页一页的显示方便使用者逐页阅读,而最基本的指令就是按空格键(space)就往下一页显示,按 b 键就会往回(back)一页显示,而且还 ...

  4. (转)Shell——基本运算符

    Shell 基本运算符 原文:http://blog.csdn.net/sinat_36053757/article/details/70319481 Shell 和其他编程语言一样,支持多种运算符, ...

  5. 牛客网Java刷题知识点之数组、链表、哈希表、 红黑二叉树

    不多说,直接上干货! 首先来说一个非常形象的例子,来说明下数组和链表. 上体育课的时候,老师说:你们站一队,每个人记住自己是第几个,我喊到几,那个人就举手,这就是数组. 老师说,你们每个人记住自己前面 ...

  6. 数据库SQL优化百万级数据库优化方案

    1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  7. 18 Command Line Tools to Monitor Linux Performance

    By Ravi Saive Under: Linux Commands, Monitoring Tools On: December 26, 2013 http://www.tecmint.com/c ...

  8. "Sorry this application cannot run under a virtual machine" Error

    错误: 运行一个程序是出现了 “sorry this application cannot run under a virtual machine” 错误. 如何解决: 控制面板-->卸载程序- ...

  9. 【Linux相识相知】任务计划和周期性任务

    在我们的生活中,有的工作是例行的,例如每年一次加薪.每年给女朋友过一次生日.每天上班都要打卡等,有的工作是临时发生的,例如明天朋友要来访,你需要准备午餐等等. 像很多例行的工作,你一旦忙起来就很容易忘 ...

  10. 【转载】BaseDao设计

    BaseDao接口设计 1 import java.util.List; /** * Dao接口,定义Dao基本操作 由BaseDaoImpl实现 * @author wht * @param < ...