【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 ...
随机推荐
- 利用freemarker生成带fusioncharts图片的word简报
/** * 利用freemarker生成带fusioncharts图片的word简报 * 烟台海颐软件技术论坛 * 作者 牟云飞 新建 * 毕业 ...
- Unity中surfaceShader的处理机制和finalColor
http://blog.csdn.net/swj524152416/article/details/52945375
- Mac Python建立简单的本地服务器
由于Mac自带Python 所以省去我们去下载了 打开终端 执行python stm-macmini:~ apple$ pythonPython 2.7.10 (default, Jul 14 20 ...
- IOS超强表格控件GMGridView
本文转载至 http://zyc-to.blog.163.com/blog/static/1715240020124235837247/ 介绍: iOS为我们提供UITableView类,实现了项目的 ...
- 【BZOJ3112】[Zjoi2013]防守战线 单纯形法
[BZOJ3112][Zjoi2013]防守战线 题解:依旧是转化成对偶问题,然后敲板子就行了~ 建完表后发现跟志愿者招募的表正好是相反的,感觉很神奇~ #include <cstdio> ...
- [转]jquery中innerWidth(),outerWidth(),outerWidth(true)和width()的区别
转自:http://www.cnblogs.com/keyi/p/5933981.html jquery中innerWidth(),outerWidth(),outerWidth(true)和wi ...
- 如何在JSTL中获取数组或者list对象的索引值(index)
<c:forEach items="${productList}" var="products" varStatus="status" ...
- (转)FFMPEG解码H264拼帧简解
http://blog.csdn.net/ikevin/article/details/7649095 H264的I帧通常 0x00 0x00 0x00 0x01 0x67 开始,到下一个帧头开始之前 ...
- HR_ROS 节点信息
https://stackoverflow.com/questions/24638063/install-node-serialport-module-on-arm-linux https://blo ...
- Vue:实践学习笔记(1)——快速使用
Vue:实践学习笔记(1)——快速使用 Vue基础知识 0.引入Vue 官方地址:Vue的官方下载地址 Vue推荐博客:keepfool 在你的程序中快速引入Vue: <!-- 开发环境版本,包 ...