【LeetCode 111_二叉树_遍历】Minimum Depth of Binary Tree

解法一:递归
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的更多相关文章
- LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)
104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...
- 【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 ...
- 【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 ...
- LeetCode OJ Minimum Depth of Binary Tree 递归求解
题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- 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 -day17 Path Sum I II & Flatten Binary Tree to Linked List & 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 ...
- 【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 ...
随机推荐
- 字符编码的发展(ASCII、Unicode、utf-8)
最近一直在看廖雪峰老师的python网上教程,python内容简单易理解,就没整理,但是字符串编码作为一直困扰自己的问题,看了几遍文章,最终还是将其整理如下,本篇博客总结自廖雪峰老师的网上教程:htt ...
- react.js 引用 NavBar 报错svg-spite-loader
Navbar iconName="false" 配置 改为 iconName={this.props.bool}
- “使用驱动器中J:的光盘之前需要将其格式化
不知道神马原因致使U盘无法打开——大家千万注意:以后遇见这种情况千万别格式化(当然如果你的U盘或者硬盘里没有重要东西那就另当别论),进入“开始-cmd”,因为我的U盘在电脑上读出来是J盘,所以在cmd ...
- 使用svn的过程中check out的文件路径中的文件图标全都加上了“蓝色问号”的解决方案
(1)你在对同一层目录下创建一个记事本文件,然后把下面这句话复制进去 for /r . %%a in (.) do @if exist "%%a\.svn" rd /s /q &q ...
- 算法+OpenCV】基于opencv的直线和曲线拟合与绘制(最小二乘法)
http://blog.csdn.net/guduruyu/article/details/72866144 最小二乘法多项式曲线拟合,是常见的曲线拟合方法,有着广泛的应用,这里在借鉴最小二乘多项式曲 ...
- 20145302张薇《Java程序设计》第六周学习总结
20145302 <Java程序设计>第六周学习总结 教材学习内容总结 第十章 串流设计的概念 无论来源和目的地实体形式是什么,只要取得InputStream和OutputStream实例 ...
- 20145307第七周JAVA学习报告
20145307<Java程序设计>第七周学习总结 教材学习内容总结 Lambda Lambda语法概述: Arrays的sort()方法可以用来排序,在使用sort()时,需要操作jav ...
- wget指定目录下载以及其它的使用方式
转自 http://java-er.com/blog/wget-useage-x/ 有时候我们需要wget一个文件下载到指定的目录下,或者重命名成指定的名字 wget -r -p -np -k -P ...
- redis:Invalid input of type: 'bool' type. Convert to a byte,string or number first
分析:出现此错误的原因是redis版本过高导致的,因此降低redis版本即可 解决: pip install -U redis==2.10.6
- <The old man and the sea>
Every day is a new day. It is better to be lucky. But i would rather be exact. Then when luck comes ...