解法一:递归

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. i.MX 6Q开发环境配置

    #适用于 Ubuntu 14.04 x64   imx6qdl-cubox-i.dtsi   #更新系统 sudo apt-get update sudo apt-get upgrade   #安装基 ...

  2. C/C++之Qt正则表达式

    引言 正则表达式(regular expression)就是用一个“字符串”来描述一个特征,然后去验证另一个“字符串”是否符合这个特征.比如 表达式“ab+” 描述的特征是“一个 'a' 和 任意个  ...

  3. TCP/IP原理简述

    TCP/IP与OSI TCP/IP工作在第4层,在TCP/IP协议簇中,有两个不同的传输协议:TCP(传输控制协议)和UDP(用户数据报协议),它们分别承载不同的应用.TCP协议提供可靠的服务,UDP ...

  4. GIT使用—补丁与钩子

    一.补丁 生成补丁 [root@localhost buding]# echo B > file;git add file;git commit -m "B" [master ...

  5. JavaScript浮点运算,小数点精度

    math.js JavaScript浮点运算,小数点精度 // JavaScript Document //数学函数 // 浮点数加法运算 function floatAdd(arg1, arg2) ...

  6. pxe基于虚拟机的自启动

    环境系统:centos6.4 min版 虚拟机实现:提供的服务器ip为192.168.0.105,桥接 安装dhcp服务: yum -y install dhcp 配置dhcp服务,使能够成功启动: ...

  7. 访问ashx一般应用程序

    浏览器中的地址栏键入要访问页面的地址:回车(是和服务器软件打交道)----向服务器发送请求(以http协议为基础,服务器按照此协议解释理解接收到的数据),服务器接收到发送的请求,根据请求信息知道当前所 ...

  8. linux下nano中复制粘贴剪切的快捷键是什么

    答: 1.复制:alt+6 2.剪切:ctrl+k 3.粘贴:ctrl+u 4.自由剪切: ctrl+6指定起始剪切位置,按上下左右键来选中内容,然后按下ctrl+k即可自由剪切 5.撤销: alt+ ...

  9. Linux 下部署Django项目

    Linux 下部署Django项目   说明:本文所使用的环境为CentOS 6+Python2.7+Django1.11 安装Django.Nginx和uWSGI 1.确定已经安装了2.7版本的Py ...

  10. spring boot将jar包转换成war包发布

    spring boot将jar包转换成war包发布步骤 将<packaging>jar</packaging>修改为<packaging>war</packa ...