[LeetCode] 110. 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.
问题:给定一个二叉树,判断其是否平衡。
当二叉树中所有节点的左右子树高度差不大于 1 时,这个二叉树视为平衡二叉树。
解题思路应该算一种分治思想。根据定义,递归地判断,实现判断。
const int unbalanceKids = -; /**
* count the number of children of node and itself
*/
int height(TreeNode* node){ int heightL = ;
if (node->left != NULL){
heightL = height(node->left);
} if(heightL == unbalanceKids){
return unbalanceKids;
} int heightR = ;
if (node->right != NULL){
heightR = height(node->right);
} if(heightR == unbalanceKids){
return unbalanceKids;
} if (abs(heightL - heightR) > ){
return unbalanceKids;
} return max(heightL, heightR) + ;
} bool isBalanced(TreeNode* root) { if (root == NULL){
return true;
} int res = height(root);
if (res == unbalanceKids){
return false;
} return true;
}
[LeetCode] 110. Balanced Binary Tree 解题思路的更多相关文章
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- Java [Leetcode 110]Balanced Binary Tree
题目描述: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced b ...
- Java for LeetCode 110 Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- [LeetCode] 110. Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- LeetCode 110. Balanced Binary Tree (平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【LeetCode】Balanced Binary Tree 解题报告
[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- Leetcode 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
随机推荐
- iOS_15_通过代码自己定义cell_微博UI
终于效果图: BeyondTableViewController.h // // BeyondTableViewController.h // 15_代码自己定义cell_weibo // // Cr ...
- setTimeout()的返回值
今天遇到一个问题,题目如下: var len=4; while(len--){ setTimeout(function(){ console.log(len); },0); console.log(l ...
- 你的第一个Windows程序——绘制窗口
MSDN原文(英文) 绘制窗口 你已经创建了你的窗口,现在你想在它里面显示东西.在WIndows术语里,这就是所谓的绘制窗口.混合隐喻,一个窗口是一个空白画布,等待你去填充它. 有时你的程序将启动绘制 ...
- 计算方法(二)用C#实现数值积分
在工程中,经常会遇到积分问题,这时原函数往往都是找不到的,因此就需要用计算方法的数值积分来求. public class Integral { /// <summary> /// 梯形公式 ...
- Swift中面向协议的编程
什么是面向协议的编程? 面向协议的编程,是一种编程范式. 编程范式,是一个计算机科学用语.维基百科中的解释是,计算机编程的基本风格或典型模式.通俗来说,就是解决某一个问题的方法不同方法和思路. 像大家 ...
- C#总结项目《影院售票系统》编写总结二
昨天发布了总结项目的第一篇,需求分析以及类的搭建,今天继续更新,动态绘制控件.票类型的切换以及数据在窗体中的展现. 先从简单的开始,票类型的切换. 分析: 1.当点击普通票时 学生折扣和赠送者是禁用的 ...
- jQuery节点操作,jQuery插入节点,jQuery删除节点,jQuery Dom操作
一.创建节点 1 var box = $('<div>节点</div>'); //创建一个节点,或者var box = "<div>节点</div& ...
- HDU_2068_RPG错排
Problem Description 今年暑假杭电ACM集训队第一次组成女生队,其中有一队叫RPG,但做为集训队成员之一的野骆驼竟然不知道RPG三个人具体是谁谁.RPG给他机会让他猜猜,第一次猜:R ...
- UITableView初始
近期在自学IOS,看了黑马提供的视频,讲的很好.在此做些笔记,以供以后查阅.注明了知识来源应该不算侵权吧. 一 UITableView 1,数据展示的条件 1⃣️ UITableView的所有数据都是 ...
- ORACLE外键和锁
在oracle中,如果外键未加索引,对父表的修改,会导致子表被加上全表锁.这包括两种情况: 1.删除父表中的行,如果外键上没有索引,会导致子表被加上全表锁 2.更新父表的主键(根据关系数据库的原则,更 ...