Balanced Binary Tree (二叉树DFS)
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 binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
int maxdepth;
public:
void solve(TreeNode *root,int depth){
if(root==NULL)
return;
if(depth>maxdepth)
maxdepth=depth;
solve(root->left,depth+);
solve(root->right,depth+);
}
int getDepth(TreeNode *root) {
maxdepth=;
solve(root,);
return maxdepth;
}
bool isBalanced(TreeNode *root) {
if(root==NULL)
return true;
int l,r;
if(root->left!=NULL){
l=getDepth(root->left);
}else{
l=;
}
if(root->right!=NULL){
r=getDepth(root->right);
}else{
r=;
}
return abs(l-r)<=&&isBalanced(root->left)&&isBalanced(root->right);
}
};
Balanced Binary Tree (二叉树DFS)的更多相关文章
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树
平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...
- LeetCode: Balanced Binary Tree 解题报告
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
随机推荐
- html----有关图像
这一节内容可概括为:网页上插入图片,调整图片大小,使用缩略图链接至大图. 图片的三种格式:jpeg png gif 照片.复杂图像使用jpeg,单色图像.logo.几何图形使用png以及 ...
- 关于Android软键盘把布局顶上去的问题(一)
最近接触到了一个登陆页面,布局最上面显示的是一个波纹的view,中间显示账号和密码的EditText,紧接着还有一个Button: 希望:点击EditText时,软键盘不能把波纹的view顶出去,也不 ...
- spark编译错误解决 Error:(52, 75) not found: value TCLIService
对于2.20版本可能会出现以下问题: spark\sql\hive-thriftserver\src\main\java\org\apache\hive\service\cli\thrift\Thri ...
- 遮罩 HUD 指示器 蒙板 弹窗
遮罩 HUD 指示器 蒙板 弹窗 UIAlertView的使用<代理方法处理按钮点击> UIAlertView *alertView = [[UIAlertView alloc] init ...
- Farseer.net轻量级开源框架 中级篇:Cookies、Session、Request
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: 探究ORM(Mapping) 下一篇:Farseer.net轻量级开源框架 中级篇 ...
- 富文本KindEditor使用
1.官网down KindEditor,添加到自己的项目中:添加时可把不需要的文件夹干掉,asp/php等等.我的项目用的是纯html和js,直接调用后台api: 2.页面引入相关js.eclipse ...
- pthread Win32多线程编程的一些知识和感想
研究遗传算法的一大诟病就是每次运行程序的结果并不是完全一样的,有时候能找到最优解有时候找不到最优解,这就是遗传算法的概率性导致的.那么怎么评价你的方法的好坏呐,这时候就要多次独立运行程序最后取结果的平 ...
- Shiro的subject实质上是当前执行用户的特定视图。
Shiro的subject实质上是当前执行用户的特定视图. 通过org.apache.shiro.SecurityUtils可以查询当前执行用户: Subject currentUser = Secu ...
- spring-shiro 配置
配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...
- arch - 显示机器的体系结构
总览 (SYNOPSIS) arch 描述 (DESCRIPTION) arch 等同于 uname -m. 目前的 Linux 系统上, arch 显示 的 数据 有 "i386" ...