[LeetCode]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 {
private:bool flag;
public:int DFS(TreeNode *root){if(root){int height1=DFS(root->left);int height2=DFS(root->right);if(abs(height1-height2)>1) flag=false;return max(height1,height2)+1;}else return 0;}bool isBalanced(TreeNode *root) {flag=true;int height=DFS(root);//树高return flag;}
};
转载于:https://www.cnblogs.com/Rosanna/p/3463596.html
[LeetCode]Balanced Binary Tree相关推荐
- leetcode - Balanced Binary Tree
题目:Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a ...
- 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 TODO
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Leetcode::Balanced Binary Tree
后序遍历,每个节点只遍历一次. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode ...
- Leetcode: Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary t ...
- LeetCode:Balanced Binary Tree
题目链接 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- LeetCode 110 Balanced Binary Tree 平衡二叉树
LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
- LeetCode 110. Balanced Binary Tree
LeetCode 110. Balanced Binary Tree 本博客参考自:http://www.cnblogs.com/grandyang/p/4045660.html Solution1: ...
最新文章
- Nginx负载均衡与配置Nginx的ssl
- canvas 圆角矩形填充_Canvas:随机长方形生成和色块填充
- PKI/CA (2)核心说明
- Object类的hashCode()方法
- controller配对与接触配对
- php判断一个字符串是否为纯数字,php判断变量是否为纯数字字符串的方法
- 黑客帝国动态特效代码
- 前端车牌识别SDK算法及原理
- 微信认证小程序如何绑定微信支付商户平台
- Python:整数的阶乘
- win7计算机节电模式,大师详解win7省电模式怎么关的方法介绍
- English-旅游英语及情景对话
- cve-2021-22205复现
- C语言编周期100ms的方波信号,单片机系统设计 - 2020学年春(邸志刚)-中国大学mooc-题库零氪...
- 【转】高级育婴师 告诉你,一定要这么做
- Put GridView inside ScrollView
- 六款Android 应用的自动化测试工具
- 苹果、天猫同步关停是因为发新品?这些猜想你看靠谱吗....
- Linux使用技巧1--挂载U盘和Windwos分区
- TCP的三次握手与四次挥手理解及面试题(很全面)
热门文章
- 监控某个dll被修改_浅谈动力环境监控系统技术标准
- Arcgis执行Raster Project时报Error001143 : Background server threw an exception
- 前端开发入门站点推荐
- kubernetes 客户端client-go 使用及常用api
- Intellij IDEA 添加jar包的三种方式
- Pixel Bender 浅尝
- SNS应用开发架构建议
- 深入理解kafka_深入理解Kafka随书代码示例已整理完毕,附地址~
- k8s service type_通过搭建MySQL掌握k8s(Kubernetes)重要概念(上):网络与持久卷...
- 如何卸载mysql5.6.28_如何完全删除MySQL以进行全新安装