第一题

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 Solution {
public int run(TreeNode root) {
if(root == null)
return 0;
int i = run(root.left);
int j = run(root.right);
return (i == 0 || j == 0) ? i+j+1 : Math.min(i, j)+1;
}
}

  这段代码是网上大神的,使用了递归的思想。

  当遇到空节点(即叶子节点的子节点)时,返回0,表示叶子节点的子树的最短深度为0(叶子节点没有子树)。对于非叶子节点,利用run函数得到其左右子树的最短深度,将其中比较短的那条子树的最短深度+1(要加上本节点)后返回,特别的,如果其左右子树中的有一条为空,则返回非空子树的最短深度+1,只是因为当存在空子树时,过此点的最小深度路径之经过非空的那条子树。当然,如果左右子树都为空,说明当前节点为叶子节,返回值为1。

leetcode-Given a binary tree, find its minimum depth的更多相关文章

  1. [LeetCode]题解(python):111 Minimum Depth of Binary Tree

    题目来源 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minim ...

  2. LeetCode之Balanced Binary Tree 平衡二叉树

    判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...

  3. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  4. LeetCode 110. Balanced Binary Tree (平衡二叉树)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  5. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  6. 【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  7. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  8. (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  9. (二叉树 递归) leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

随机推荐

  1. Hadoop+HBase 集群搭建

    Hadoop+HBase 集群搭建 1. 环境准备 说明:本次集群搭建使用系统版本Centos 7.5 ,软件版本 V3.1.1. 1.1 配置说明 本次集群搭建共三台机器,具体说明下: 主机名 IP ...

  2. 关于TF(词频) 和TF-IDF(词频-逆向文件频率 )的理解

    ##TF-IDF TF(词频):  假定存在一份有N个词的文件A,其中‘明星‘这个词出现的次数为T.那么 TF = T/N; 所以表示为: 某一个词在某一个文件中出现的频率. TF-IDF(词频-逆向 ...

  3. Linux安装NET CORE

    Linux安装.NET CORE 1.Add the dotnet apt-get feed 为了在Ubuntu或Linux Mint上安装.NET,您需要首先设置托管所需软件包的apt-get fe ...

  4. Dubbo 分布式服务框架简介

    1.分布式服务框架 1.1 Dubbo 简介 Dubbo 是一个分布式服务框架,以及阿里巴巴内部的 SOA 服务化治理方案的核心框架.其功能主要包括:高性能 NIO 通讯及多协议集成,服务动态寻址与路 ...

  5. A*算法详解链接

    A星算法详解(个人认为最详细,最通俗易懂的一个版本) Introduction to the A* Algorithm 路径规划: a star, A星算法详解 实现A星算法

  6. slfj+logback

    1.pom.xml <dependency> <groupId>org.projectlombok</groupId> <artifactId>lomb ...

  7. idea checkout 项目

    1. 2.添加一个连接 选择远程目录checkout  , 然后选择本地目录   但是这样会创建一个新的projectwindow  如果要创建为module的话  1.手动引入从svn check的 ...

  8. C++ char 类型:字符型和最小的整型

    C++ 中没有 byte,Java 中有 byte. 但是 C++ 有 char,char 是可用来放整数的最小字节类型. #include <iostream> int main() { ...

  9. shell md5sum

    md5sum  out.a echo "fd_limit=$(ulimit -n), fd_used=$(ll /proc/4741/fd | wc -l)"   需找句柄,及fd ...

  10. Angular4中路由Router类的跳转navigate

    最近一直在学习angular4,它确实比以前有了很大的变化和改进,好多地方也不是那么容易就能理解,好在官方的文档和例子是中文,对英文不太好的还是有很大帮助去学习. 官方地址:https://angul ...