[LeetCode 111] - 二叉树的最小深度 (Minimum Depth of Binary Tree)
问题
给出一棵二叉树,找出它的最小深度。
最小深度是指从根节点沿着最短路径下降到最近的叶子节点所经过的节点数。
初始思路
不难看出又是一个需要层次遍历二叉树的题目,只要在112基础上作出简单修改即可得出答案。
class Solution
{
public:
int minDepth(TreeNode *root)
{
if(!root)
{
return ;
} treeLevel_[].clear();
treeLevel_[].clear(); depth_ = ; bool flag = false;
treeLevel_[].push_back(root); while(!treeLevel_[flag].empty())
{
++depth_;
for(auto iter = treeLevel_[flag].begin(); iter != treeLevel_[flag].end(); ++iter)
{
if(!(*iter)->left && !(*iter)->right)
{
return depth_;
} if((*iter)->left)
{
treeLevel_[!flag].push_back((*iter)->left);
} if((*iter)->right)
{
treeLevel_[!flag].push_back((*iter)->right);
}
} treeLevel_[flag].clear(); flag = !flag;
} return depth_; } private:
std::vector<TreeNode*> treeLevel_[];
int depth_;
};
minDepth
[LeetCode 111] - 二叉树的最小深度 (Minimum Depth of Binary Tree)的更多相关文章
- [Swift]LeetCode111. 二叉树的最小深度 | 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 111_二叉树_遍历】Minimum Depth of Binary Tree
解法一:递归 int minDepth(TreeNode* root) { if (root == NULL) ; if (root->left == NULL) { ; } else if ( ...
- Java实现 LeetCode 111 二叉树的最小深度
111. 二叉树的最小深度 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,nu ...
- leetcode 111. 二叉树的最小深度
题目描述: 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null, ...
- 每日一题-——LeetCode(111)二叉树的最小深度
题目描述: 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 思路一: 把每一层的结点加入到队列,每一层i+1,到下一层时,把上一层在队列中的结点都弹出,按从 ...
- leetcode 111二叉树的最小深度
使用深度优先搜索:时间复杂度O(n),空间复杂度O(logn) /** * Definition for a binary tree node. * struct TreeNode { * int v ...
- 【LeetCode 104_二叉树_遍历】Maximum Depth of Binary Tree
解法一:递归 int maxDepth(TreeNode* root) { if (root == NULL) ; int max_left_Depth = maxDepth(root->lef ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- centos 6.2 关闭 IPV6
在现在的Linux上IPv6已经在默认安装下被支持,但是对于一些对IPv6支持不是很好的应用服务器来说,开启了IPv6反而会影响服务器的网络性能,毕竟现在的网络交换设备不是IPv6的. 如何判断系统是 ...
- 使用EMOJI表情
因为IOS系统支持日文中的字块编码,所以在UILable,UITextField,UIAlertView等控件中使用emoji表情编码(emoji就是表情符号:词义来自日语(えもじ,e-moji,mo ...
- iPhone 各版本屏幕分辨率
参考:http://www.paintcodeapp.com/news/iphone-6-screens-demystified
- Ubuntu ssh的使用
1. 题外话:install teamviewer on Ubuntu. wget http://download.teamviewer.com/download/teamviewer_linux.d ...
- iscc2016 pwn部分writeup
一.pwn1 简单的32位栈溢出,定位溢出点后即可写exp gdb-peda$ r Starting program: /usr/iscc/pwn1 C'mon pwn me : AAA%AAsAAB ...
- linux系统应用--Linux下用virtualBox安装win7(共享文件夹)
1. deepin终端: sudo apt-get install virtualbox 2. 下载win7 iso文件 3. deepin终端启动virtualbox : ./virtualbo ...
- 【iOS开发】emoji表情的输入思路
1.自定义一个表情包类继承NSTextAttachment #import <UIKit/UIKit.h> /** 表情包的自定义类*/ @interface EmojiTextAttac ...
- 使用多线程完成Socket
public class Service { //服务器 public static void main(String[] args) { ServerSocket serverSocket=null ...
- css08盒子模型
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- memcache缓存命中深入理解转载
http://www.iteye.com/topic/225692 memcache的方法有 add,set,replace,get,delete,getstats,increment,decreme ...