【Leetcode】【Easy】Balanced Binary Tree
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的更多相关文章
- 【LeetCode】Balanced Binary Tree 算法优化 解题报告
Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【一天一道LeetCode】#104. Maximum Depth of Binary Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
随机推荐
- [转] 使用Gson进行json数据转换list to json 和json to list
[From] https://blog.csdn.net/god2030/article/details/51140450 经过比较,gson和其他现有java json类库最大的不同时gson需要序 ...
- Java static{}语句块详解
[转自] http://blog.csdn.net/lubiaopan/article/details/4802430 static{}(即static块),会在类被加载的时候执行且仅会被执行一次,一 ...
- 编辑距离及编辑距离算法(求字符的相似度) js版
编辑距离概念描述: 编辑距离,又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符. 例如 ...
- 神马是代码简单的cmd模式,这就是!
小狼正在研究 “怎么查找连在一起的同色方块?”算法问题 ,突然感觉我是不是需要一种开发模式,不然感觉自己的代码好乱的. 可能是研究算法吧,导致小狼的思路特别清晰,加上也用了差不多1年的nodejs.s ...
- nginx+uwsgi+virtualenv+supervisor部署项目
一.导论 WSGI是Web服务器网关接口.它是一个规范,描述了Web服务器如何与Web应用程序通信,以及Web应用程序如何链接在一起以处理一个请求,(接收请求,处理请求,响应请求) 基于wsgi运行的 ...
- drf之视图案例
views.py from django.shortcuts import render # Create your views here. from rest_framework.generics ...
- pycharm中使用正则表达式批量添加print括号,完美从python2迁移到python3
网络下载的python代码,版本参差,从python2.x迁移python3.x的过程中,存在print语法问题,即python2.x中print无括号,python3.x中print有括号. 逐行添 ...
- mongodb 增查改删
我们在 MongoDB 之 你得知道MongoDB是个什么鬼 MongoDB - 1 中学习了如果安装部署一个 MongoDB 如果没看到我的金玉良言的话,就重新打开一次客户端和服务端吧 本章我们 ...
- linux中安装软件,查看、卸载已安装软件方法
各种主流Linux发行版都采用了某种形式的包管理系统(PMS)来控制软件和库的安装. 软件包存储在服务器上,可以利用本地Linux系统上的PMS工具通过互联网访问.这些服务器称为仓库. 由于Linux ...
- shell的常用脚本一
批量创建用户名脚本: ######################################################################### # File Name: cr ...