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

    电场磁场仿真软件安装出现问题: 基本问题都一样: 解决方式1:安装路径不要有中文的路径. 若安装提示vc++2005x86 安装失败 问题是:没有安装vc++2005 请安装vc++2005 x86 ...

  2. 2016(4)数据库系统,ER模型,规范化理论,并发控制

    试题四(共25分) 阅读以下关于数据库设计的叙述,在答题纸上回答问题1至问题3. 某航空公司要开发一个订票信息处理系统,以方便各个代理商销售机票.开发小组经过设计,给出该系统的部分关系模式如下: 航班 ...

  3. 虚拟机上不了网的 VMware Workstation 与 Device/Credential Guard 不兼容

    VMware Workstation 与 Device/Credential Guard 不兼容   和    虚拟机上不了网的问题  解决方法: VMware Workstation 与 Devic ...

  4. Redis集群方案怎么做?

    转载自:https://www.jianshu.com/p/1ecbd1a88924 Redis集群方案 Redis数据量日益增大,而且使用的公司越来越多,不仅用于做缓存,同时趋向于存储这块,这样必促 ...

  5. 【QT】qt python install pip

    https://pip.pypa.io/en/stable/installing/ http://www.runoob.com/w3cnote/python-pip-install-usage.htm ...

  6. 简介一下 i++和++i&&i=i+i,i+=1;的区别

    首先: int i=2; i++=2; ++i=3; 前者先显示当前的值,而后者则是先自增在显示值: second i=i+1和i+=1; 输出结果虽然一样,但是 1.byte i=2; i+=2; ...

  7. 【JavaScript】事件捕获、事件冒泡与事件委托

    2018年12月18日 最近在学习js时,遇到了三个名词:事件捕获.事件冒泡.事件委托. 一.事件捕获和事件冒泡 事件捕获和事件冒泡是为了解决网页中的事件流(事件发生的顺序)而提出的概念. 事件捕获是 ...

  8. mysqlworkbench访问远程服务器

    1.如果服务器有防火墙,需要关一下: systemctl stop firewalld.service #停止firewallsystemctl disable firewalld.service # ...

  9. .Net与 WebAssembly 随笔

    WebAssembly 是啥 - WebAssembly 是一种浏览器支持的字节码格式,WebAssembly 字节码和底层机器码很相似可快速装载运行,因此性能相对于 JS 解释执行大大提升,需要放到 ...

  10. 使用google earth engine根据NDWI(归一化水指数)提取水体信息

    交流合作请联系: ab000c@163.com