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

Example

Example 1:

Input: {}
Output: 0

Example 2:

Input:  {1,#,2,3}
Output: 3
Explanation:
1
\
2
/
3
 
注意:
如果用普通的递归,那么在helper方法之前要考虑只有一边树的情况(根结点+右子树 root.left == null && root.right != null /跟节点+左子树 root.right == null && root.left != null )。一定要加&&,否则input为{2}(只有跟节点时)也会进入这个case。如果不考虑一边树的话,会误判左子树的长度为1,那么会minDepth会返回1.
递归法代码:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/**
* @param root: The root of binary tree
* @return: An integer
*/
int minDepth = Integer.MAX_VALUE;
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
} else if (root.left == null && root.right != null) {
helper(root.right, 2);
} else if (root.right == null && root.left != null) {
helper(root.left, 2);
} else {
helper(root, 1);
}
return minDepth;
}
public void helper(TreeNode root, int curDepth) {
if (root == null) {
return;
}
if (root.left == null && root.right == null) {
if (curDepth < minDepth) {
minDepth = curDepth;
}
return;
} helper(root.left, curDepth + 1);
helper(root.right, curDepth + 1);
}
}
二分法:
1. 需要返回值
2. 自下而上求解(把问题分解成若干小问题)
public class Solution {
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
return getMin(root);
} public int getMin(TreeNode root){
if (root == null) {
return Integer.MAX_VALUE;
} if (root.left == null && root.right == null) {
return 1;
} return Math.min(getMin(root.left), getMin(root.right)) + 1;
}
}

Lintcode155-Minimum Depth of Binary Tree-Easy的更多相关文章

  1. 【leetcode】Minimum Depth of Binary Tree (easy)

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

  2. LeetCode_111. Minimum Depth of Binary Tree

    111. Minimum Depth of Binary Tree Easy Given a binary tree, find its minimum depth. The minimum dept ...

  3. LeetCode OJ Minimum Depth of Binary Tree 递归求解

        题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...

  4. leetcode -day17 Path Sum I II &amp; Flatten Binary Tree to Linked List &amp; Minimum Depth of Binary Tree

    1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such tha ...

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

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

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

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

  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 My Solution: Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...

随机推荐

  1. 百度地图API实时画出动态运行轨迹(一条行驶轨迹),车头实时指向行驶方向,设置角度偏移

    参考网址:https://blog.csdn.net/skywqnan/article/details/79036262 改变车的方向:http://www.cnblogs.com/peixuanzh ...

  2. Java数组之二维数组

    Java中除了一维数组外,还有二维数组,三维数组等多维数组.本文以介绍二维数组来了解多维数组. 1.二维数组的基础 二维数组的定义:二维数组就是数组的数组,数组里的元素也是数组. 二维数组表示行列二维 ...

  3. win openssl 生成证书

    第1步:生成私钥 有密码:openssl genrsa -des3 -out private.key 1024无密码:openssl genrsa -out private.key 1024 说明:生 ...

  4. 一个简单小技巧实现手机访问.html文件网页效果

    注册登录Github不解释 settings设置往下拉 选择一个主题上传代码文件到code 打开这个文件选择此时的网址 在网址前面加上 这段代码 http://htmlpreview.github.i ...

  5. 前端多选插件bootstrap-select的使用

    一.分别引入bootstrap-select.min.js和bootstrap-select.min.css文件 二.在页面中写一个class为selectpicker的select控件 <se ...

  6. Aliyun OSS Nginx proxy module(阿里云OSS Nginx 签名代理模块)

    1.此文章主要介绍内容 本文主要介绍如何利用Nginx lua 实现将阿里云OSS存储空间做到同本地磁盘一样使用.核心是利用Nginx lua 对OSS请求进行签名并利用内部跳转将所有访问本地Ngin ...

  7. SSIS Hekaton In-Memory OLTP 【翻译一篇外国文章】

    来自:http://www.itprotoday.com/microsoft-sql-server/important-new-features-sql-server-2014 Microsoft's ...

  8. Kali-Dos洪水攻击之Hping3

    在计算机行业,拒绝服务(DoS)或分布式拒绝服务(DDoS)攻击是指不法分子企图让某机器或网络资源无法被预期的用户所使用.虽然执行DoS攻击的方式.动机和目标不一样,但通常包括设法临时性或无限期中断或 ...

  9. html5css练习 旋转

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  10. python的对象 变量

    对象 所有的数据类型都是对象 字符串 数字:整数.数字 列表.字典 函数.类.模块.方法 print(type(20)) # 查看对象的数据类型 python整型: int  浮点型 :float 精 ...