【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.
题解:递归解决,用私有变量minDep保存最小的深度,每当遍历到叶节点的时候就看是否需要更新。
代码如下:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private int minDep = Integer.MAX_VALUE;
public void minDepthRecur(TreeNode root,int depth){
if(root == null)
return;
if(root.left == null && root.right == null){
if(minDep > depth)
minDep = depth;
return;
}
minDepthRecur(root.left, depth+1);
minDepthRecur(root.right, depth+1);
}
public int minDepth(TreeNode root) {
if(root == null)
return 0;
minDepthRecur(root, 1);
return minDep;
}
}
【leetcode刷题笔记】Minimum Depth of Binary Tree的更多相关文章
- [刷题] 104 Maximum Depth of Binary Tree
要求 求一棵二叉树的最高深度 思路 递归地求左右子树的最高深度 实现 1 Definition for a binary tree node. 2 struct TreeNode { 3 int va ...
- 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 s ...
- 力扣算法题—111.Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the sh ...
- 【leetcode❤python】 111. Minimum Depth of Binary Tree
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
- leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- leetcode刷题-559. Maximum Depth of N-ary Tree
题目: https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/ n-ary-tree的数据结果表示 // Defi ...
- [leetcode刷题笔记]Implement Trie (Prefix Tree)
题目链接 一A,开森- ac代码: class TrieNode { // Initialize your data structure here. char content; boolean isW ...
- 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 ...
- 【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 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- Unable to VNC onto Centos server remotely
用的好好的vncserver 突然遇到这个错误: [vnc@localhost ~]$ sudo systemctl status vncserver@:1.service -l● vncserver ...
- Laravel手记:执行route:cache时报LogicException
laravel5的路由支持缓存.需要执行以下命令: php artisan route:cache 执行完毕后,报出以下错误: Route cache cleared! [LogicException ...
- 安装Hadoop 1.1.2 (一 安装JDK)
1 下载jdk1.7 xxx .rpm 2 以Root权限登陆 3 修改文件权限 chmod +x jdk-7u25-linux-x64.rpm 4 安装 JDK rpm -ivh jdk-7u2 ...
- android菜鸟学习笔记24----与服务器端交互(一)使用HttpURLConnection和HttpClient请求服务端数据
主要是基于HTTP协议与服务端进行交互. 涉及到的类和接口有:URL.HttpURLConnection.HttpClient等 URL: 使用一个String类型的url构造一个URL对象,如: U ...
- how to add them, how to multiply them
http://www.physics.miami.edu/~nearing/mathmethods/operators.pdf
- python数据分析之:时间序列二
将Timestamp转换为Period 通过使用to_period方法,可以将由时间戳索引的Series和DataFrame对象转换为以时期索引 rng=pd.date_range('1/1/2000 ...
- 如何从统计中批量获取BD搜索关键词及对应的入口页面?
前面我们介绍了通过cnzz的访问明细获取到搜索关键词及对应的入口页面,但是从BD搜索进来的关键词无法完整显示,只能呈现一些bd图片搜索的关键词,这是因为百度宣布从去年5月开始逐渐取消了referer关 ...
- bug-3——onload,onbeforeunload,Onunload的区别
window.onload事件设置页面加载时执行的动作,即进入页面的时候执行的动作. window.onunload已经从服务器上读到了需要加载的新的页面,在即将替换掉当前页面时调用 一般用于设置 ...
- 解决oracle锁表
1.查看被锁住的表select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects ...
- valuestack,stackContext,ActionContext.之间的关系以及action的数据在页面中取得的方法
转自:http://blog.csdn.net/quechao123/article/details/4406148 1.三者之间的关系如下图所示: 2.action的数据在页面中取得的方法 在st ...