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 ...
随机推荐
- C++程序员笔试复习概要(一)
第8章 类和对象的创建 [内容提要] 类和对象 构造函数和析构函数 对象数组与对象指针 静态成员 友元 静态函数 虚函数 [重点与难点] 8.1 类和对象 8.1.1 类的定义 类实质上是用户 ...
- Postgresql 技巧
备份 pg_dump -f "F:/dump.sql"<file name> -U postgres<database name> -h 10.38.197 ...
- IOS 学习参考
IOS 开发 http://code4app.com/ios/%E5%AE%9E%E6%97%B6%E6%9B%B4%E6%96%B0%E7%9A%84%E6%9B%B2%E7%BA%BF%E5%9B ...
- RedHat6.4 用UDEV配置ASM所需磁盘
同事在装一套RAC测试环境,结果发现原来用ASMLIB来配置磁盘,在安装GRID的时候,最终报错了,经过检查发现居然两边的磁盘不匹配.A机的sdb,sdc分别对应OCRVOL1,OCRVOL2,但是B ...
- js 判断页面加载状态
//----判断当前页面是否加载状态 开始 ---- document.onreadystatechange = subSomething;//当页面加载状态改变的时候执行这个方法. function ...
- dive into python 读笔(1)
chapter2 and 3: 使用Python IDE来交互式地测试表达式 编写Python程序并且从IDE运行,或者从命令行运行 导入模块及调用它们的函数 声明函数以及doc string.局部变 ...
- 字符串截取 方法 String b=a.substring(0, a.indexOf("乘坐"));
String b=a.substring(0, a.indexOf("乘坐"));
- 【DP/单调栈】关于单调栈的一些题目(codevs 1159,codevs 2673)
CODEVS 2673:Special Judge 题目描述 Description 这个月的pku月赛某陈没有参加,因为当时学校在考试[某陈经常逃课,但某陈还没有强大到考试也可以逃掉的程度].何 ...
- redhat_suse双系统引导
先装suse11系统,再装redhat6后出现引导中suse系统无法启动解决方法:在redhat中将suse所在分区挂载出来,找其boot/grub/menu.lst下的启动项,将该启动项复制到red ...
- tornado做简单socket服务器(TCP)
http://blog.csdn.net/chenggong2dm/article/details/9041181 服务器端代码如下: #! /usr/bin/env python #coding=u ...