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.
题目解答:判断一棵给定的二叉树是不是平衡二叉树。平衡二叉树的条件有:
它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。
对于树的处理,一般都使用递归的方式。
代码:
/**
* 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;
if((root -> left == NULL ) && (root -> right == NULL))
return true;
int leftDepth = 0;
int rightDepth = 0;
return isSubBalance(root -> left,leftDepth) && isSubBalance(root -> right , rightDepth) && (abs(leftDepth - rightDepth) <= 1) ;
}
bool isSubBalance(TreeNode *root,int &depth)
{
if(root == NULL)
{
depth = 0;
return true;
}
if((root -> left == NULL ) && (root -> right == NULL))
{
depth = 1;
return true;
}
else
{
depth = 1;
int leftDepth = 0;
int rightDepth = 0;
return isSubBalance(root -> left,leftDepth) && isSubBalance(root -> right , rightDepth) && (abs(leftDepth - rightDepth) <= 1) && (depth += max(leftDepth,rightDepth) ) ;
}
}
};
转载于:https://www.cnblogs.com/CodingGirl121/p/5425720.html
Leetcode题目:Balanced Binary Tree相关推荐
- LeetCode 110 Balanced Binary Tree
LeetCode 110 Balanced Binary Tree Problem Description: 判断二叉树是不是平衡二叉树.所谓平衡二叉树,即每个节点的两个子树深度差的绝对值不超过1. ...
- 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
LeetCode 110. Balanced Binary Tree 本博客参考自:http://www.cnblogs.com/grandyang/p/4045660.html Solution1: ...
- 【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 平衡二叉树
110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5055 ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. 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: 106. Construct Binary Tree from Inorder and Postorder Traversal
题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...
最新文章
- 【转】apache常用配置
- R语言如何将字符串转变为命令执行
- 设计模式 — 行为型模式 — 策略模式
- 强/若类型语言 动/静态语言
- Android短信发送流程之普通短信发送(原)
- nginx核心模块常用指令
- 安卓学习-WebView
- 数据结构期末复习之选择排序
- 流利说公布上市后首份财报:第三季净收入1.8亿
- 使用静态库的一些问题 -all_load
- FFMPEG 常用命令行
- paip.网站提示SESSION过期 登录过期 以及二次登录的问题
- html 中加载字体太慢,css字体文件包太大无法引入怎么处理?
- 双十一数码好物指南,双十一不踩坑推荐
- 创建一个urdf机器人_ROS机器人Diego制作16-创建机器人的urdf模型描述文件
- 计算机开机慢更新配置,系统开机慢?教你如何提高电脑开机速度!
- 20135220谈愈敏-期末总结
- 企业上云业务系统上云是怎么回事儿?
- php tree view,VB.Net树视图(TreeView)
- 调用notifyDataSetChanged无法更新ListView