/**
* 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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [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 ...

  4. LeetCode - Minimum Depth of Binary Tree

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  5. [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 ...

  6. leetcode Minimum Depth of Binary Tree python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  7. 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 ...

  8. LeetCode Minimum Depth of Binary Tree 找最小深度(返回最小深度)

    题意:找到离根结点最近的叶子结点的那一层(设同一层上的结点与根结点的距离相等),返回它所在的层数. 方法有: 1.递归深度搜索 2.层次搜索 方法一:递归(无优化) /** * Definition ...

  9. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  10. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

随机推荐

  1. 巨坑npm run dev 报错 终于找到正确答案 Error: EPERM: operation not permitted, open '/data/public/build/css/add.p

    Windows10环境 npm run dev 报错  终于找到正确答案 Error: EPERM: operation not permitted, open '/data/public/build ...

  2. 记一次非常规方法对接硬件设备(Grason Stadler GSI 61)

    Grason Stadler GSI 61 电测听设备 (写下设备的名字, 希望别人遇坑可以搜到) 对接说明 设备厂家提供自带的软件,但是没有找到接口说明.我们需要获取设备发送过来的数据. 厂家提供的 ...

  3. 支持不同Android设备,包括:不同尺寸屏幕、不同屏幕密度、不同系统设置

    Some of the important variations that you should consider include different languages, screen sizes, ...

  4. 使用snap

    snap是一个Linux上的包管理器,其目的是提供跨平台的包管理 提到包管理我们会想到python的 pip conda等,以及 apt等等 snap提供了一个 统一的体验在各种Linux发行版上 关 ...

  5. Android 第四次作业

    一.团队成员: 段嗣跃:https://www.cnblogs.com/duansiyue/ 陈素伟:https://www.cnblogs.com/aX-qhu/ 二.APK链接: https:// ...

  6. 洛谷p3799:妖梦切木棒

    题意:任选四段木板拼正三角形 因为是正三角形 所以我们可以想到至少是两个相同的,剩下两个拼成最后一条边 我们只需要枚举边长即可 那么我们对每次读入的x,使他的cnt++ 考虑用一个二重循环 外层枚举边 ...

  7. 使用curl上传图片的方法

    关键:当参数名为"@绝对路径",这时 CURL 會幫你做 multipart/form-data 編碼 实现方法: $params = array( 'file' => '@ ...

  8. redis的过期策略都有哪些?

    1.面试题 redis的过期策略都有哪些?内存淘汰机制都有哪些?手写一下LRU代码实现? 2.面试官心里分析 1)老师啊,我往redis里写的数据怎么没了? 之前有同学问过我,说我们生产环境的redi ...

  9. IIS 接口访问404

    IIS->网站->ASP->启用父路径(true)

  10. kettle web化

    kettle web化 通过Java API调用kettle核心代码,并基于Spring Boot提供简易的Web管理界面. 背景 在工作中,通过kettle这款ETL产品进行数据处理时,是通过kit ...