[LeetCode 110] - 平衡二叉树 (Balanced Binary Tree)
问题
给出一棵二叉树,判断它是否在高度上是平衡的。
对于本问题,高度上平衡的二叉树定义为:每个节点的两棵子树的深度差永远不大于1的一棵二叉树。
初始思路
根据定义,思路应该比较直接:递归计算每个节点左右子树的深度,只要发现一次深度差大于1的情况,即可终止递归返回不平衡的结果。最终代码如下:
class Solution
{
public:
bool isBalanced(TreeNode *root)
{
if(!root)
{
return true;
} isBalanced_ = true; GetDepth(root); return isBalanced_;
} private:
int GetDepth(TreeNode* node)
{
//已经不平衡后不需要再继续了,返回的深度也没用了,随便返回一个0即可
if(!isBalanced_)
{
return ;
} if(!node->left && !node->right)
{
return ;
} //当前节点贡献1的深度
int left = ;
int right = ; if(node->left)
{
left += GetDepth(node->left);
} if(node->right)
{
right += GetDepth(node->right);
} if(left > right + || right > left + )
{
isBalanced_ = false; return ;
} return left > right ? left : right;
} bool isBalanced_; };
isBalanced
[LeetCode 110] - 平衡二叉树 (Balanced Binary Tree)的更多相关文章
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
- 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树
平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...
- LeetCode OJ: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 bina ...
- 【LeetCode OJ】Balanced Binary Tree
Problem Link: http://oj.leetcode.com/problems/balanced-binary-tree/ We use a recursive auxilar funct ...
- [Swift]LeetCode110. 平衡二叉树 | Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 平衡二叉树Balanced Binary Tree
[抄题]: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced b ...
- LeetCode算法题-Balanced Binary Tree(Java实现)
这是悦乐书的第167次更新,第169篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第26题(顺位题号是110).给定二叉树,判断它是否是高度平衡的.对于此问题,高度平衡二 ...
- LeetCode题解之Balanced Binary Tree
1.题目描述 2.问题分析 DFS. 3.代码 bool isBalanced(TreeNode* root) { if (root == NULL) return true; && ...
随机推荐
- wxPython学习笔记(三)
要理解事件,我们需要知道哪些术语? 事件(event):在你的应用程序期间发生的事情,它要求有一个响应. 事件对象(event object):在wxPython中,它具体代表一个事件,其中包括了事件 ...
- Android-语言设置流程分析
Android手机语言切换行为,是通过设置-语言和输入法-语言来改变手机的语言,其实这个功能很少被用户使用. 以Android5.1工程源码为基础,从设置app入手来分析和学习语言切换的过程: ...
- 使用Httpwatch分析响应时间--转
时间片段名称 意思 Blocked (阻塞)灰色 阻塞的时间主要包括,预处理时间,(如缓存查找)和网络排队等待的时间,导致阻塞最主要的原因是下载页面中的图片 DNS Lookup(域名解释)紫色 域名 ...
- [转] linux下的僵尸进程处理SIGCHLD信号
什么是僵尸进程? 首先内核会释放终止进程(调用了exit系统调用)所使用的所有存储区,关闭所有打开的文件等,但内核为每一个终止子进程保存了一定量的信息.这些 信息至少包括进程ID,进程的终止状态,以及 ...
- The requested page cannot be accessed because the related configuration data for the page is invalid
当在VS2013下开发web site时,调试时都是在IIS Express中进行的,没有问题.当部署到IIS中,出现:The requested page cannot be accessed be ...
- junit测试用例加载spring配置文件
junit加载pom引用项目的xml配置文件,如果定义了<beans profile="dev">,必须在测试用例类上面加上标记 @ActiveProfiles(&qu ...
- 基于脚本的动画的计时控制(“requestAnimationFrame”)(转)
requestAnimationFrame 方法的支持,该方法通过在系统准备好绘制动画帧时调用该帧,从而为创建动画网页提供了一种更平滑更高效的方法.在此 API 之前,使用 setTimeout 和 ...
- c# 预处理命令
在编译之前进行的处理. 预处理命令以符号“#”开头. #define 只能 定义符号 不能定义宏(#define PI 3.14 这是错的,在c#中没宏) #region #endregion #if ...
- android studio adb 打不开
1.cmd-->C:\Users\Administrator>adb start-serveradb server is out of date. killing...error: cou ...
- RMAN-06496: must use the TO clause when the database is mounted or open
一:问题描述 备份控制文件时报错: RMAN> restore controlfile from '/home/oracle/backup/PROD_32_20140829.bak'; Star ...