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.
递归,求左右是否平衡,再判断高度差
/**
* 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 checkBalance(TreeNode *node, int &dep)
{
if (node == NULL)
{
dep = ;
return true;
} int leftDep, rightDep;
bool leftBalance = checkBalance(node->left, leftDep);
bool rightBalance = checkBalance(node->right, rightDep); dep = max(leftDep, rightDep)+; return leftBalance && rightBalance && (abs(rightDep - leftDep) <= );
}
bool isBalanced(TreeNode *root) {
int dep;
return checkBalance(root, dep); }
};
Balanced Binary Tree——经典题的更多相关文章
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...
- [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树
4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 平衡二叉树(Balanced Binary Tree)
平衡二叉树(Balanced Binary Tree)/AVL树:
- [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II
Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...
随机推荐
- pushViewController:animated:的问题
1.在AppDelegate.m中: 2.在SecondViewController.h中: 3.在FirstViewController.m中: 4.在SecondViewController.m中 ...
- dubbo介绍以及创建
1.什么是dubbo? DUBBO是一个分布式服务框架(关于框架,其实就是配置文件加java代码),致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天为2 ...
- sshSSH Secure Shell Client root用户无法登录解决办法
最近使用这个工具,普通用户可以登录root用户不可以登录.将vi /etc/ssh/sshd_config按照下述配置解决问题 修改sshd配置文件:vi /etc/ssh/sshd_config P ...
- 51Nod 1381 硬币游戏 | 概率(数学期望)
Input 第一行给出一个整数T,表示有T组数据(1<=T<=10000). 第2行到T+1,每行给出一个整数R.(0< R <= 10,000,000,000) Output ...
- JAVA开发常用工具包
一个有经验的Java开发人员特征之一就是善于使用已有的轮子来造车.<Effective Java>的作者Joshua Bloch曾经说过:“建议使用现有的API来开发,而不是重复造轮子”. ...
- freemarker中空值“”,null值的判断
原文:http://zhousheng193.iteye.com/blog/1319772 <#if letVo.manageScore!=""> ${html('${ ...
- UOJ#21 【UR #1】缩进优化
传送门 http://uoj.ac/problem/21 枚举 (调和级数?) $\sum_{i=1}^{n} (a_i / x + a_i \bmod x) =\sum a_i - (\sum_{i ...
- 【洛谷 P4735】 最大异或和 (可持久化Trie)
题目链接 维护整个数列的异或前缀和和\(s\),然后每次就是要求\(s[N]\text{^}x\text{^}s[k],l-1<=k<=r-1\)的最大值 如果没有\(l\)的限制,那么直 ...
- HDU 2639 Bone Collector II (dp)
题目链接 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took part in ...
- python初步学习-python控制流
语句书写规范 缩进在python语言书写中非常重要,如果缩进不规范,执行程序将会报错 引用维基百科中的叙述: Python開發者有意讓違反了縮排規則的程序不能通過編譯,以此來強迫程序員養成良好的編程習 ...