Leetcode 之Balanced Binary Tree(49)

用递归的方式来做,左右两棵子树的高度差不超过1。分成两部分,一部分递归得到树的高度,一部分递归检查左右子树是否是平衡二叉树。
int getHeight(TreeNode *root)
{
if (root == nullptr)return ;
return max(getHeight(root->left), getHeight(root->right)) + ;
}
bool isBalancedTree(TreeNode *root)
{
if (root == nullptr)return true;
if (root->left == nullptr && root->right == nullptr)return true;
if (abs(getHeight(root->left) - getHeight(root->right)) > )return false; return(isBalancedTree(root->left) && isBalancedTree(root->right));
}
Leetcode 之Balanced Binary Tree(49)的更多相关文章
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- 【LeetCode】Balanced Binary Tree 算法优化 解题报告
Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- [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 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- LeetCode之Balanced Binary Tree 平衡二叉树
判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a 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】Balanced Binary Tree(平衡二叉树)
这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...
- Leetcode 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
随机推荐
- [bzoj] 1068 压缩 || 区间dp
原题 f[i][j][0/1]表示i-1处有一个M,i到j压缩后的长度,0/1表示i到j中有没有m. 初始为j-i+1 f[i][j][0]=min(f[i][j][0],f[i][k][0]+j-k ...
- lamp 源码安装
#!/bin/bash #description:mysql-.tar apache2.4.23 php5.6.27 function check_ok(){ ] then echo "-- ...
- Educational Codeforces Round 6 A
A. Professor GukiZ's Robot time limit per test 0.5 seconds memory limit per test 256 megabytes input ...
- Ubuntu Android adb调试无法识别设备 -- List of devices attached ???????????? no permissions
在Ubuntu下做Android开发, 使用adb devices调试的时候出现在面的错误: 这个问题之前就出现过的,突然就忘了,Mark一下.在网上找了一下,基本上是一些比较麻烦的办法,但是在我的记 ...
- zabbix问题汇总
1. ::110809.577 resuming IPMI checks on host [10.1.3.41]: connection restored :: seconds :: seconds ...
- [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- 51Nod 1049最大子段和 | 模板
Input示例 6 -2 11 -4 13 -5 -2 Output示例 20 1.最大子段和模板 #include "bits/stdc++.h" using namespace ...
- 【洛谷 P1419】 寻找段落(二分答案,单调队列)
题目链接 开始还以为是尺取.发现行不通. 一看标签二分答案,恍然大悟. 二分一个\(mid\)(实数),把数列里每个数减去\(mid\),然后求前缀和,在用单调队列维护\(sum[i-t\text{~ ...
- 【洛谷 P3402】 【模板】可持久化并查集
题目链接 可持久化并查集,就是用可持久化线段树维护每个版本每个节点的父亲,这样显然是不能路径压缩的,否则我们需要恢复太多状态. 但是这并不影响我们启发式合并,于是,每次把深度小的连通块向深度大的上并就 ...
- fs.createReadStream(filepath).pipe(response);这句是什么意思?
'use strict'; var fs = require('fs'), url = require('url'), path = require('path'), http = require(' ...