LeetCode Balanced Binary Tree (判断平衡树)
题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1。
思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool ans;//答案会记录在此。
int DFS(TreeNode* root)
{
if(!root || !ans) return ; //已经错了,不必要再比较下去
int b=DFS(root->right);
int a=DFS(root->left);
if(abs(a - b)>) ans=false;
return max(a,b)+;
} bool isBalanced(TreeNode* root) {
ans=true;//初始化答案
DFS(root);
return ans;
}
};
AC代码
LeetCode Balanced Binary Tree (判断平衡树)的更多相关文章
- LeetCode: Balanced Binary Tree 解题报告
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- LeetCode——Balanced Binary Tree(判断是否平衡二叉树)
问题: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- balanced binary tree(判断是否是平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- [leetcode]Balanced Binary Tree @ Python
原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/ 题意:判断一颗二叉树是否是平衡二叉树. 解题思路:在这道题里,平衡二叉树的定义是二 ...
- LeetCode——Balanced Binary Tree
Description: Given a binary tree, determine if it is height-balanced. For this problem, a height-bal ...
- [Leetcode] 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 binary ...
- LeetCode - Balanced Binary Tree
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- 【easy】110. Balanced Binary Tree判断二叉树是否平衡
判断二叉树是否平衡 a height-balanced binary tree is defined as a binary tree in which the depth of the two su ...
随机推荐
- [gradle] is applicable for argument types
error: is applicable for argument types: (org.eclipse.jetty.server.Request) 很显然这个错误是因为 不是静态方法造成的,改为静 ...
- JSON操作,转载
http://www.cnblogs.com/mcgrady/archive/2013/06/08/3127781.html#_label0
- Head First设计模式悟道
暂时包括 策略模式,观察者,装饰模式,工厂模式,抽象工厂模式,后续会继续补充中,纯属个人总结用,不喜勿喷, 源代码见: 传送门 public class NYPizzaIngredientFactor ...
- NSInvocation的使用(转)
转载自:http://www.cnblogs.com/pengyingh/articles/2359199.html http://blog.iosxcode4.com/?p=125 在 iOS中可以 ...
- Objective-C程序结构及语法特点
程序文件分为头文件(.h)和实现文件(.m): 使用#import关键字将所需的头文件导入程序,并且可以避免程序重复引用相同的头文件: @autoreleasepool { … } 自动释放池: 符号 ...
- 关于android内存泄漏的研究
博客建了几个月,都没有去写,一是因为当时换工作,然后又是新入职(你懂的,好好表现),比较忙:二是也因为自己没有写博客的习惯了.现在还算是比较稳定了,加上这个迭代基本也快结束了,有点时间来写写博客.好了 ...
- js 判断文件是否存在(转载)
js 判断文件是否存在(转载) var fso,s=filespec; // filespec="C:/path/myfile.txt"fso=new ActiveXObject ...
- Mybatis 示例之 SelectKey(转)
参考:http://blog.csdn.net/isea533/article/details/21153791 SelectKey在Mybatis中是为了解决Insert数据时不支持主键自动生成的问 ...
- HibernateTemplate、HibernateDaoSupport两种方法实现增删改查Good(转)
Spring+Hibernate两种方法实现增删改查 首先,定义一个Customer的bean类,设置好Customer.hbm.xml文件.再定义好一个Dao接口.准备好一个jdbc.propert ...
- Extjs-4.2.1(二)——使用Ext.define自定义类
鸣谢:http://www.cnblogs.com/youring2/archive/2013/08/22/3274135.html --------------------------------- ...