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. 关于vue2非表单元素使用contenteditable="true"实现textarea高度自适应

    <template> <div ref="sendContent" contenteditable="true" v-html="s ...

  2. eclipse F6和F8的问题

    # 首先大致说明一下F6和F8的作用: | 在debug模式下, F6的作用是跳到下一步,F8的作用是跳到下一个断点 # 情景: | 在eclipse以debug模式同时启动两个项目,并且两个项目都打 ...

  3. mysqldump 命令使用

    常见选项:--all-databases, -A: 备份所有数据库--databases, -B: 用于备份多个数据库,如果没有该选项,mysqldump把第一个名字参数作为数据库名,后面的作为表名. ...

  4. python 管理多版本之pyenv

    一, [root@management ~]# pyenv install -listAvailable versions:  3.3.0  3.3.1  3.3.2  3.3.3  3.3.4  3 ...

  5. JAVA 中 if和while的区别

    while和if本身就用法不同,一个是循环语句,一个是判断语句. if 只做判断,判断一次之后,便不会再回来了while 的话,循环,直到结果为false,才跳出来 链表的结构,要一直读下去,直到读完 ...

  6. angular的基本要点

    <body ng-app="Myapp"> <div ng-controller="firstcon"> <h1>hello ...

  7. Beam编程系列之Python SDK Quickstart(官网的推荐步骤)

    不多说,直接上干货! https://beam.apache.org/get-started/quickstart-py/ Beam编程系列之Java SDK Quickstart(官网的推荐步骤)

  8. TOJ 2711 Stars

    描述 Astronomers often examine star maps where stars are represented by points on a plane and each sta ...

  9. Android ContentProvider的介绍(很详细)

    博客分类: android进阶   一.ContentProvider的概念 ContentProvider:为存储和获取数据提供统一的接口.可以在不同的应用程序之间共享数据.Android已经为常见 ...

  10. centos6.5下搭建oracle 11g

    1.安装依赖 sudo yum install binutils compat-libstdc++-33 compat-libstdc++-33.i686 elfutils-libelf elfuti ...