Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1.
我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树
/**
* 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:
int depth(TreeNode* root){
if(!root) return ;
else{
return max(depth(root->left), depth(root->right)) + ;
}
}
bool isBalanced(TreeNode* root) {
if(!root) return true;
else if(abs(depth(root->left) - depth(root->right))> ){
return false;
}
else return isBalanced(root->left) && isBalanced(root->right);
}
};
Leetcode 110 Balanced Binary Tree 二叉树的更多相关文章
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- [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 解题思路
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)
翻译 给定一个二叉树,决定它是否是高度平衡的. (高度是名词不是形容词-- 对于这个问题.一个高度平衡二叉树被定义为: 这棵树的每一个节点的两个子树的深度差不能超过1. 原文 Given a bina ...
- leetcode 110 Balanced Binary Tree(DFS)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode 110. Balanced Binary Tree平衡二叉树 (C++)
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- Leetcode 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
随机推荐
- 请不要做浮躁的IT人
1.不要看到别人的回复第一句话就说:给个代码吧!你应该想想为什么.当你自己想出来再参考别人的提示,你就知道自己和别人思路的差异. 2.初学者请不要看太多太多的书那会误人子弟的,先找本系统的学,很多人用 ...
- HttpModule的一些初步认识
新建一个类 ValidaterHttpModuleEvents继承管道接口 IHttpModule,代码如下 public class ValidaterHttpModuleEvents:IHttpM ...
- andorid SQLite数据库创建文件
package com.hanqi.application3; import android.content.ContentValues; import android.database.sqlite ...
- js截取字符串显示引号两种方法
//截取字符串多余显示引号 var cutStrForNum = function (str, num) { var len = 0; for (var i = 0; i < str.lengt ...
- mormot json操作
使用JSon只需要引用一个文件synCommons. procedure TForm1.Button1Click(Sender: TObject);var jo: Variant; i: Int64; ...
- Java实现多线程的两种方式
实现多线程的两种方式: 方式1: 继承Thread类 A: 自定义MyThread类继承Thread类 B: 在MyThread类中重写run() C: 创建MyThread类的对象 D: 启动线程对 ...
- opps kio
Unable to handle kernel NULL pointer dereference at virtual address 00000008pgd = c7090000, hw pgd = ...
- css小技巧之去掉蓝色底块的方法
-moz-user-select: none; /*火狐*/ -webkit-user-select: none; /*webkit浏览器*/ -ms-user-select: none; /*IE1 ...
- servers中添加server时,看不到运行环境的选择。
servers中添加server时,看不到运行环境的选择. 主要原因是tomcat目录中的配置文件格式不对.
- 不要轻易delete void*指针,这样会隐藏比较多的错误。
#include<iostream> using namespace std; class Object{ void* data; const int size; const char i ...