二叉树基本功练习题,题目如下:

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.

就是找最短的那条从根到叶,跟上一道题一样直接那递归上就行。解法如下:

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void minDepth(TreeNode *temp, int count, int &min)
{
if (temp != NULL)
{
if (temp->left == NULL && temp->right == NULL)
{
min = min > count ? count : min;
} minDepth(temp->left, count + 1, min);
minDepth(temp->right, count + 1, min);
}
}
int minDepth(TreeNode *root) {
if (root == NULL)
{
return 0;
}
int min = 2147483647;
minDepth(root, 1, min);
return min;
}
};

就是这样,拿count来计数,然后把min的地址传参进去,每到底部叶子的时候判断一次大小。

[leetcode] 5. Minimum Depth of Binary Tree的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  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] 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 ...

  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 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

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

  8. leetcode 111 minimum depth of binary tree

    problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...

  9. 【leetcode】Minimum Depth of Binary Tree

    题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...

  10. 【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 ...

随机推荐

  1. vim配置之tagbar

    vimConfig/plugin/tagbar-setting.vim let g:tagbar_width=4 map <F12> :TagbarToggle<CR> map ...

  2. Java之POI的excel导入导出

    一.Apache POI是一种流行的API,它允许程序员使用Java程序创建,修改和显示MS Office文件.这由Apache软件基金会开发使用Java分布式设计或修改Microsoft Offic ...

  3. PG自动化测试

    安装软件包 yum groupinstall "Development Tools" yum install zlib-devel tcl-devel readline-devel ...

  4. SQL Server修改表结构后批量更新所有视图

    最近修改了数据库表结构,数据同步的时候出了问题,发现很多数据明明已经修改,但是通过视图筛选出来的还是原来的数据,所以怀疑应该是视图缓存了数据,在园子里找到下面的博文,在这里做个记录备忘. 原文链接:h ...

  5. Hyberledger-Fabric 1.00 RPC学习(1)

    参考:http://hyperledger-fabric.readthedocs.io/en/latest/write_first_app.html 本文的目的就是基于Hyperledger Fabr ...

  6. curl获取响应时间

    1.开启gzip请求curl -I http://www.sina.com.cn/ -H Accept-Encoding:gzip,defalte 2.监控网页的响应时间curl -o /dev/nu ...

  7. C#实现图片文件到数据流再到图片文件的转换

    //----引入必要的命名空间using System.IO;using System.Drawing.Imaging; //----代码部分----// private byte[] photo;/ ...

  8. 2.spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除

    转自:http://www.importnew.com/23358.html 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天主 ...

  9. 可视化库-Matplotlib-3D图(第四天)

    1. 画三维图片图 axes = Axes3D(fig)这一步将二维坐标转换为三维坐标,axes.plot_surface() import matplotlib.pyplot as plt impo ...

  10. ffmpeg源码分析--av_find_best_stream <转>

    1. av_find_best_streama. 就是要获取音视频及字幕的stream_indexb.以前没有函数av_find_best_stream时,获取索引可以通过如下 ; i<is-& ...