解法一:递归

int minDepth(TreeNode* root)
{
if (root == NULL)
return ; if (root->left == NULL) {
return minDepth(root->right) + ;
} else if (root->right == NULL) {
return minDepth(root->left) + ;
} else {
int left_minDepth = minDepth(root->left);
int right_minDepth = minDepth(root->right);
return left_minDepth < right_minDepth ? left_minDepth + : right_minDepth + ;
}
}

解法二:BFS

int minDepth(TreeNode *root)
{
if (root == NULL)
return ; queue<TreeNode*> node_queue;
node_queue.push(root); int count = ;
while (!node_queue.empty()) {
int len = node_queue.size();
count++;
for (int i = ; i < len; ++i) {
TreeNode *nodeTmp = node_queue.front();
node_queue.pop(); if (nodeTmp->left)
node_queue.push(nodeTmp->left);
if (nodeTmp->right)
node_queue.push(nodeTmp->right);
if (nodeTmp->left == NULL && nodeTmp->right == NULL)
return count;
}
}
}

【LeetCode 111_二叉树_遍历】Minimum Depth of Binary Tree的更多相关文章

  1. LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)

    104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...

  2. 【Leetcode】【Easy】Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

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

  4. LeetCode OJ Minimum Depth of Binary Tree 递归求解

        题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...

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

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

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

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

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

  8. leetcode -day17 Path Sum I II &amp; Flatten Binary Tree to Linked List &amp; Minimum Depth of Binary Tree

    1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such tha ...

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

随机推荐

  1. 【转】安装Vue.js的方法

    安装vue.js的方法   一.简介 Vue.js 是什么 Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的 渐进式框架.与其他重量级框架不同的是,Vue 采用自底向上增量 ...

  2. jQuery height() 需要注意的地方

    var aNode = $('#id'); var height = aNode.height(); //如果在获取height前,aNode已经是display:none 或者说 aNode是隐藏的 ...

  3. 快用Visual Studio(二)- 界面,功能区与命令面板

    Layout Editing Explorer Saving Searching Command Palette File Navigation File and Folder Based Files ...

  4. yarn命令使用

    yarn 常用命令 修改日期 2017.12.26 最初接触 yarn 还是在 0.17.10 版本,由于各种各样的原因,使用时没 npm 顺手, 目前 yarn 的版本已经升级为 1.3.2 各种之 ...

  5. keras安装配置指南【linux环境】【转】

    本文转载自:https://keras-cn.readthedocs.io/en/latest/for_beginners/keras_linux/#kerasmnist 本教程不得用于任何形式的商业 ...

  6. MysQL使用一高级应用(下)

    连接查询 连接查询分类如下: 表A inner join 表B:表A与表B匹配的行会出现在结果中 表A left join 表B:表A与表B匹配的行会出现在结果中,外加表A中独有的数据,未对应的数据使 ...

  7. JAVA基础补漏--泛型通配符

    泛型通配符只能用于方法的参数 不能用对象定义 public class Test { public static void main(String[] args) { ArrayList<Str ...

  8. 搭建springmvc

    对于SpringMvc来说,搭建这框架,首先引入相应的包.如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xml ...

  9. 跟着我一起学习大数据——Hadoop

    hadoop配置文件:http://archive.cloudera.com/cdh5/cdh/5/hadoop-2.6.0-cdh5.9.0/ 一:Hadoop简介 总结下起源于Nutch项目,社区 ...

  10. [Network Architecture]DPN(Dual Path Network)算法详解(转)

    https://blog.csdn.net/u014380165/article/details/75676216 论文:Dual Path Networks 论文链接:https://arxiv.o ...