【LeetCode】Balanced Binary Tree(平衡二叉树)
这道题是LeetCode里的第110道题。
题目要求:
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
示例 1:
给定二叉树
[3,9,20,null,null,15,7]3
/ \
9 20
/ \
15 7返回
true。
示例 2:给定二叉树
[1,2,2,3,3,null,null,4,4]1
/ \
2 2
/ \
3 3
/ \
4 4返回
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:
bool isBalanced(TreeNode* root) {
if(root==NULL)return true;
int l_len=Depth(root->left);
int r_len=Depth(root->right);
if(abs(l_len-r_len)<=1&&
isBalanced(root->left)&&
isBalanced(root->right))
return true;
return false;
}
int Depth(TreeNode* root){
if(root==NULL)return 0;
return max(Depth(root->left),Depth(root->right))+1;
}
};
提交结果:

个人总结:
这道题用迭代法似乎挺难的还没具体的思路,如果树的深度越深,则开销就越大,要处理的左右子树也很多。
【LeetCode】Balanced Binary Tree(平衡二叉树)的更多相关文章
- [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 ...
- [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: Balanced Binary Tree 解题报告
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- [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(判断是否平衡二叉树)
问题: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- LeetCode之Balanced Binary Tree 平衡二叉树
判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...
- LeetCode 110. Balanced Binary Tree平衡二叉树 (C++)
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- [leetcode]Balanced Binary Tree @ Python
原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/ 题意:判断一颗二叉树是否是平衡二叉树. 解题思路:在这道题里,平衡二叉树的定义是二 ...
随机推荐
- Ubuntu 16.04 server版本开机启动脚本不支持
Ubuntu16.04开机启动的脚本一直不支持,错误用在将开机启动脚本放到了home/usr/的目录下,应该放到/root才能正常启动.#!/bin/sh -e ## rc.local## This ...
- 一键部署Drupal开源内容管理系统
https://market.azure.cn/Vhd/Show?vhdId=10897&version=12950 产品详情 产品介绍Drupal是一个由Dries Buytaert创立的自 ...
- Python+selenium之fixtures
fixtures即可以表示测试用例的开始和结束,也可以表示测试类和测试模块的开始和结束. import unittest def setUpModule(): print("test mod ...
- jmeter参考网址
http://blog.csdn.net/dongdong9223/article/details/49248979 http://blog.csdn.net/hjh00/article/detail ...
- UVA 11732 strcmp() Anyone (Trie+链表)
先两两比较,比较次数是两者相同的最长前缀长度*2+1,比较特殊的情况是两者完全相同时候比较次数是单词长度*2+2, 两个单词'末尾\0'和'\0'比较一次,s尾部'\0'和循环内'\0'比较一次. 因 ...
- 剑指offer58 二叉树的下一个结点
自己写的 class Solution { public: TreeLinkNode* GetNext(TreeLinkNode* pNode) { if(pNode == NULL) return ...
- Spring boot 配置异步处理执行器
示例如下: 1. 新建Maven 项目 async-executor 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...
- Python 的多态与多态性
多态:是指一类事物有多种形态(!!!!定义角度!!!!) 多态性:在继承的基础上, (!!!!使用角度!!!!!) 使用多态性,实现了利用函数统一调用一个接口 多态 #多态:同一种事物的多种形态,动物 ...
- 用户输入和while循环
函数input()的工作原理 message=input('Tell me something,and I will repeat it back to you:') print(message) 编 ...
- php的字符转换 & php登入注册界面设计以及源码 & 分离公共部分
我们在编写的时候总是会出现乱码 https://www.cnblogs.com/mafeng/p/5827215.html php登入注册界面设计以及源码 https://blog.csdn.net/ ...