33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree
OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
思想:先序遍历。注意的是: 当只有一个孩子结点时,深度是此孩子结点深度加 1 .
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode *root) {
if(root == NULL) return 0;
if(root->left == NULL && root->right == NULL) return 1;
int l = minDepth(root->left);
int r = minDepth(root->right);
return (l && r) ? min(l, r) + 1 : (l+r+1);
}
};
Balanced Binary Tree
OJ: https://oj.leetcode.com/problems/minimum-depth-of-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.
思想: 先序遍历。既要返回左右子树判断的结果,又要返回左右子树的深度进行再判断。
所以要么返回一个 pair<bool, int>, 要么函数参数增加一个引用来传递返回值。
方法1:返回一个 pair<bool, int>: (更简洁)
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
typedef pair<bool, int> Pair;
Pair judge(TreeNode *root) {
if(root == NULL) return Pair(true, 0);
Pair L = judge(root->left);
Pair R = judge(root->right);
return Pair(L.first && R.first && abs(L.second-R.second) < 2, max(L.second, R.second)+1);
}
class Solution {
public:
bool isBalanced(TreeNode *root) {
return judge(root).first;
}
};
方法二: 增加一个引用
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
bool judge(TreeNode *root, int& depth) {
if(root == NULL) { depth = 0; return true; }
int l, r;
if(judge(root->left, l) && judge(root->right, r)) {
depth = 1 + max(l, r);
if(l-r <= 1 && l-r >= -1) return true;
else return false;
}
}
class Solution {
public:
bool isBalanced(TreeNode *root) {
int depth;
return judge(root, depth);
}
};
Maximum Depth of Binary Tree
OJ: https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
注: 此题略水,于此带过。
class Solution {
public:
int maxDepth(TreeNode *root) {
return root ? max(maxDepth(root->left), maxDepth(root->right))+1 : 0;
}
};
33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree的更多相关文章
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- Leetcode | Minimum/Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- [LeetCode#104, 111]Maximum Depth of Binary Tree, Minimum Depth of Binary Tree
The problem 1: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes ...
- [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 ...
- [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 ...
- Maximum Depth of Binary Tree 解答
Question Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along ...
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- (二叉树 BFS DFS) 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 ...
随机推荐
- XML学习摘要
XML元素可以在开始标签中包含属性. 属性(Attribute)提供关于元素的额外信息,属性必须加引号. 属性值必须被引号包围,不过单引号和双引号均可,若属性值本身包含双引号,那么有必要使用单引号包围 ...
- 设计模式六大原则(5)—迪米特法则
定义: 一个对象应该对其它的对象保持最少的了解.迪米特法则又称为最少知识法则,英文全称为Least Knowledge Principle ,简称为LKP. 个人理解: 迪米特法则主要目的是类间解耦, ...
- Jmeter—2 http请求—简单的get请求
发送一个简单的get http请求 1 启动Jmeter,在测试计划上点击鼠标右键>添加>Threads(Users)>线程组 2 线程组界面.可设置线程数,几秒启动所有线程,循环次 ...
- JavaScript基础--DOM对象加强篇(十四)
1.document 对象 定义:document对象代表的整个html文档,因此可以去访问到文档中的各个对象(元素)document重要的函数 1.1 write 向文档输出文本或js代码 1.2 ...
- hdu 2095
ps:真是日了狗...英语渣渣理解题目不行,开了个100W数组来算,还优化了下时间,还是超时了,看了题解才知道用异或. N个数异或,会得出其中是奇数的一个.比如 1^1^3^2^2 = 3. 1^ ...
- PATH变量
PATH变量由系统管理员配置,通常包含如下一些存储系统程序的标准路径: /bin 二进制文件目录,用于存放启动系统时用到的路径 /usr/bin 用户二进制文件目录,用于存放用户使用的标准程序 / ...
- 基于cocos2d-x的Android游戏中使用fmod音频引擎
cocos2d-x的音频引擎是cocosDenshion, 它的Android版比较弱, 只能播放一个背景音乐和些许音效, 如果要实现稍微复杂一点的音频播放, 比如同时播放几个音轨就不能了. 这一点远 ...
- (实用篇)jQuery+PHP+MySQL实现二级联动下拉菜单
二级联动下拉菜单选择应用在在很多地方,比如说省市下拉联动,商品大小类下拉选择联动.本文将通过实例讲解使用jQuery+PHP+MySQL来实现大小分类二级下拉联动效果. 先看下效果 大类: 前端技术 ...
- MySQL安装及主从配置
系统环境:CentOS release 6.5 (Final)(最小化安装) MySQL版本:mysql-5.6.12 Cmake版本:cmake-2.8.4 说明:安装mysql先安装cmake(原 ...
- POI给Excel添加数字筛选
HSSFSheet sheet = workbook.createSheet("list"); CellRangeAddress ce = CellRangeAddress.val ...