LeetCode 110. 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.
Example 1:
Given the following tree [3,9,20,null,null,15,7]
:
3
/ \
9 20
/ \
15 7
Return true.
Example 2:
Given the following tree [1,2,2,3,3,null,null,4,4]
:
1
/ \
2 2
/ \
3 3
/ \
4 4
Return false.
/**
* 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 == NULL) return ;
return max(depth(root->left), depth(root->right)) + ;
} bool isBalanced(TreeNode *root)
{
if (root == NULL) return true; int left = depth(root->left);
int right = depth(root->right); return abs(left - right) <= && isBalanced(root->left) && isBalanced(root->right); }
};
LeetCode 110. Balanced Binary Tree(判断平衡二叉树)的更多相关文章
- 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 ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- [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平衡二叉树 (C++)
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- leetcode 110 Balanced Binary Tree ----- java
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 ...
随机推荐
- web服务器-apache
一.apache详解 1. 概述 apache是世界上使用排名第一的web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的web服务器端软件之一.它快 ...
- Go语言IDE远程连接Linux服务器
我因为在自己的云服务器上面进行Go语言开发,IDE必不可少,为了减少对于服务器的压力决定使用golang远程连接进行开发: 首先准备goland https://www.jetbrains.com/g ...
- Mock Server之flask_restful(python+flask)
一.结构设计 上一篇,写了Mock Server的基础实现与被测系统的对接 当我们要mock 的 api越来越多的时候,路由与相关的方法都堆在app.py中就不合适了,不可拔插,可读性也比较差,因此要 ...
- 性能测试基础---联机负载&IP欺骗
·联机负载&IP欺骗 ·联机负载:又叫分布式负载,即通过多台负载机(压力机)运行脚本,向服务器发送请求,从而实现更多的负载压力. ·联机负载的具体操作: ·了解两个概念: ·控制机:所谓控制机 ...
- 【比赛游记】THUSC2019酱油记
往期回顾:THUWC2019酱油记 时间过得真快呐-- 上次在 THUSC 手玩 AI 的情景还未走远,明天却要迎来全新一年的赛事了-- 掐指一算,作为一个真正的 OIer 的时光也不多了啊 day ...
- P1092 虫食算[搜索]
这个式子是是由\(A\sim A+N\)组成的,那么\(A\sim A+N\)就只能等于\(0\sim N-1\),因此我们每次对\(A\sim A+N\)的取值做一个新的排列,然后judge一下当前 ...
- 利用requests库访问网站
1.关于requests库 函数 Response对象包含服务器返回的所有信息,也包含请求的Request信息. 访问百度二十次 import requests def getHTMLText(url ...
- wordpress文章显示同一分类下的上一篇下一篇
我们在用wordpress开发网站的时候会在文章页中引入上一篇下一篇,但是发现新闻页的上下文章有可能是产品分类的post,这个就不太合理,如何显示同一分类下的上一篇下一篇文章呢?随ytkah一起来看看 ...
- Arduino在串口监视器上输出字母表
程序会在Arduino IDE的串口监视器上输出一个字母表. 不需要额外电路,但是板子必须通过串口线或USB线连接到电脑. 代码 程序在setup()函数中建立串口连接,然后逐行输出a~z的字母I表, ...
- 解决:C++ 中 main函数 wmain函数 _tmain函数 WinMain函数 wWInMain函数 _tWinMain函数的区别
main函数与WinMain函数区别: 前者为控制台程序入口主函数,后者为Windows API窗体程序入口函数,在windef.h文件中定义. main函数与wmain函数 | WinMain函数与 ...