【JAVA】【leetcode】【查找二叉树最小深度】
题目: 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 TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
} public class Solution {
public int run(TreeNode root) {
if(root!=null){
int left = Integer.MAX_VALUE;
int right = Integer.MAX_VALUE;
if(root.left!=null){
left = run(root.left);
}
if(root.right!=null){
right = run(root.right);
}
if(left<right){
return left+1;
}
else if(left>right){
return right+1;
}
else if(left==right&&left!=Integer.MAX_VALUE){
return left+1;
}
else
return 1;
}
return 0; }
}
当然,还有一个较简洁的递归方法,最核心的思想就是根节点到达最近的叶子节点的路径长度:
1、当根为空时,输出0
2、当左子树为空时,输出右子树深度+1
3、当右子树为空时,输出左子树深度+1
4、以上条件都不满足时,输出min(左子树深度,右子树深度)+1
public class Solution {
public int run(TreeNode root) {
if(root==null)
return 0;
if(root.left==null)
return run(root.right)+1;
if(root.right==null)
return run(root.left)+1;
return Math.min(run(root.left),run(root.right))+1; }
}
【JAVA】【leetcode】【查找二叉树最小深度】的更多相关文章
- Java实现查找二叉树&C++的做法
写了个Java的查找二叉树,用递归做的,不用递归的还没弄出来.先贴一下.回头再研究. BTreeTest.java: public class BTreeTest{ class Node{ Node ...
- [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 ...
- LinCode 刷题 之二叉树最小深度
http://www.lintcode.com/zh-cn/problem/minimum-depth-of-binary-tree/ 题目描述信息 /** * Definition of Tree ...
- leetcode-111. 二叉树最小深度 · Tree + 递归
题面 找出二叉树的最小深度(从根节点到某个叶子节点路径上的节点个数最小). 算法 算法参照二叉树的最大深度,这里需要注意的是当某节点的左右孩子都存在时,就返回左右子树的最小深度:如果不都存在,就需要返 ...
- LeetCode111_求二叉树最小深度(二叉树问题)
题目: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the s ...
- 剑指offer-第六章面试中的各项能力(二叉树的深度)
题目:1:输入一个二叉树,求二叉树的深度.从根节点开始最长的路径. 思路:我们可以考虑用递归,求最长的路径实际上就是求根节点的左右子树中较长的一个然后再加上1. 题目2:输入一颗二叉树的根节点,判断该 ...
- Java实现 LeetCode 111 二叉树的最小深度
111. 二叉树的最小深度 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,nu ...
- LeetCode 111. 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] 111. Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
随机推荐
- Html的智能表单
-新的输入类型 -email -url -number -range -Date pickers ( date ,month ,week, time , datetime ,datetime-loca ...
- 161230、利用代理中间件实现大规模Redis集群
前面在<大规模互联网应用Redis架构要点>和<Redis官方集群方案 Redis Cluster>两篇文章中分别介绍了多Redis服务器集群的两种方式,它们是基于客户端sha ...
- windows下自动FTP的脚本
之前发过一篇如何自动FTP的文章,不过当时的脚本都是在Unix下测试通过的.而如果在windows下实现自动FTP的功能,则需要通过如下方式: 1. 建立ftp123.bat文件 open I ...
- python :添加的内容具有之前的功能用delegate绑定事件
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- three.js初涉略(一)
<!-- 最近要研究一下webgl,发现了webgl中文网(http://www.hewebgl.com/article/articledir/1).边研究教程边做下记录 --> thre ...
- int型、long型和long long型
long long本质上还是整型,只不过是一种超长的整型.int型:32位整型,取值范围为-2^31 ~ (2^31 - 1) . long:在32位系统是32位整型,取值范围为-2^31 ~ (2^ ...
- sql常用单行函数
学到数据库了,小记一下的喽~~~>>>>常用的单行函数 select * from employees 查询所有 select first_name,lower(first_n ...
- phpMyAdmin的配置
好久没写东西了,上来记录一下今天学的一点小东西吧~ 之前搞php开发的时候,一直用的是SQLyog来操作mysql数据库的,但是今天发现sqlyog功能不是很完善,主要是我想修改数据库名,但是sqly ...
- JZ2440开发板学习 1. 刚接触开发板, 安装驱动
一. jtag和串口驱动 1. 驱动下载 http://pan.baidu.com/s/1pJkAHJH 2. 驱动安装 禁用win10的驱动数字签名 http://www.100ask.org/bb ...
- Using FreeMarker templates (FTL)- Tutorial
Lars Vogel, (c) 2012, 2016 vogella GmbHVersion 1.4,06.10.2016 Table of Contents 1. Introduction to F ...