leetcode — minimum-depth-of-binary-tree
/**
* Source : 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.
*/
public class MinimumDepth {
/**
* 求出一棵二叉树的最小高度
*
* @param root
* @return
*/
public int minDepth (TreeNode root) {
if (root == null) {
return 0;
}
int left = minDepth(root.leftChild);
int right = minDepth(root.rightChild);
if (left > right) {
return right + 1;
} else {
return left + 1;
}
}
public TreeNode createTree (char[] treeArr) {
TreeNode[] tree = new TreeNode[treeArr.length];
for (int i = 0; i < treeArr.length; i++) {
if (treeArr[i] == '#') {
tree[i] = null;
continue;
}
tree[i] = new TreeNode(treeArr[i]-'0');
}
int pos = 0;
for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
if (tree[i] != null) {
tree[i].leftChild = tree[++pos];
if (pos < treeArr.length-1) {
tree[i].rightChild = tree[++pos];
}
}
}
return tree[0];
}
private class TreeNode {
TreeNode leftChild;
TreeNode rightChild;
int value;
public TreeNode(int value) {
this.value = value;
}
public TreeNode() {
}
}
public static void main(String[] args) {
MinimumDepth minimumDepth = new MinimumDepth();
char[] arr0 = new char[]{'#'};
char[] arr1 = new char[]{'3','9','2','#','#','1','7'};
char[] arr2 = new char[]{'3','9','2','1','6','1','7','5'};
System.out.println(minimumDepth.minDepth(minimumDepth.createTree(arr0)));
System.out.println(minimumDepth.minDepth(minimumDepth.createTree(arr1)));
System.out.println(minimumDepth.minDepth(minimumDepth.createTree(arr2)));
}
}
leetcode — minimum-depth-of-binary-tree的更多相关文章
- 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 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 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- LeetCode - Minimum Depth of Binary Tree
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- [LeetCode] 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 Minimum Depth of Binary Tree python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- leetcode:Minimum Depth of Binary Tree【Python版】
1.类中递归调用添加self: 2.root为None,返回0 3.root不为None,root左右孩子为None,返回1 4.返回l和r最小深度,l和r初始为极大值: # Definition f ...
- LeetCode Minimum Depth of Binary Tree 找最小深度(返回最小深度)
题意:找到离根结点最近的叶子结点的那一层(设同一层上的结点与根结点的距离相等),返回它所在的层数. 方法有: 1.递归深度搜索 2.层次搜索 方法一:递归(无优化) /** * Definition ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
随机推荐
- FCC学习笔记(一)
除了像素,你还可以使用百分比来指定border-radius边框半径的值. 给你的猫咪图片一个50%的border-radius. a元素,也叫anchor(锚点)元素,既可以用来链接到外部地址实现页 ...
- 用generator改写ajax
function request(url) { // this is where we're hiding the asynchronicity, // away from the main code ...
- mpvue-docs基于vue来开发微信小程序
http://mpvue.com/和https://tencent.github.io/wepy/
- 马昕璐/唐月晨 《面向对象程序设计(java)》第十一周学习总结
一:理论部分. 一般将数据结构分为两大类:线性数据结构和非线性数据结构 线性数据结构:线性表.栈.队列.串.数组和文件 非线性数据结构:树和图. 线性表:1.所有数据元素在同一个线性表中必须是相同的数 ...
- cf 1142 C
忽然觉得这个题很有必要写题解. 移项 y-x^2 = bx+c 那么其实就是找有多少条直线上方没有点 所以就是一个上凸壳有多少条直线/点. 绝妙啊!!!! 太妙了啊!!!! 神乎其技卧槽!!! (我是 ...
- 离线安装多版本node,使用nvm管理
windows环境下,使用nvm客户以方便地管理多个node版本,但有时候可能需要离线安装node版本. 结合网络搜搜索结果,多次尝试后我成功在离线安装了多个node版本,方法: 1.在其他联网环境下 ...
- 别以为真懂Openstack: 虚拟机创建的50个步骤和100个知识点(3)
四.Nova-compute 步骤17:nova-compute接收到请求后,通过Resource Tracker将创建虚拟机所需要的资源声明占用 步骤18:调用Neutron API配置Networ ...
- 安卓开发学习笔记(七):仿写腾讯QQ登录注册界面
这段代码的关键主要是在我们的相对布局以及线性布局上面,我们首先在总体布局里设置为线性布局,然后再在里面设置为相对布局,这是一个十分常见的XML布局模式. 废话不多说,直接上代码:一.activity. ...
- YOU种你来丨i春秋校园行第一站北京电子科技学院
发通告啦 i春秋互联网安全校园行即将火爆开启,第一站我们将来到北京电子科技学院.对网络安全感兴趣的大学生们注意啦,我们将走进校园送福利,Are you ready? i春秋简介 i春秋拥有全国最大的网 ...
- [Swift]LeetCode472. 连接词 | Concatenated Words
Given a list of words (without duplicates), please write a program that returns all concatenated wor ...