【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 ...
随机推荐
- NIO 01 (转)
本文转自:http://www.cnblogs.com/littlehann/p/3720396.html 目录 1. NIO.NIO.2简介 2. NIO中的关键技术 1. NIO.NIO.2简介 ...
- 【Linux学习】2.Linux常见命令行
记录学习Linux 系统的相关知识点,欢迎大家拍砖交流,一起成长:QQ:2712192471 作者背景:前端开发工程师 | Python | web安全爱好者 Linux命令行: 系统关机重启 s ...
- docker issue-Cannot connect to the Docker daemon. Is 'docker -d' running on this host?
Here is my docker version when i run docker version : Client: Version: 1.8.1 API version: 1.20 Go ve ...
- python3_UUID模块详解
1.知识背景 UUID是128位的全局唯一标识符,通常有32字节的字母表示.它可以保证时间和空间的唯一性. UUID——Universally unique identifier 在python中叫U ...
- php 中处理 websocket
http://www.cnblogs.com/hustskyking/p/websocket-with-php.html 下面我画了一个图演示 client 和 server 之间建立 websock ...
- 自定义圆形头像CircleImageView的使用和源码分析
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0806/3268.html tools:context="com.ex ...
- 【Beginning Python】抽象(未完)
[懒惰即是美德] 抽象意味着良好的可读性:说明你在努力做什么,而不是给出你正在如何做的细节. [抽象和结构] 程序应该是非常抽象的,就像“下载网页.计算频率.打印每个单词的频率”一样易懂.翻译成程序就 ...
- OpenStack之Keystone模块
一.Keystone介绍 OpenStack Identity(Keystone)服务为运行OpenStack Compute上的OpenStack云提供了认证和管理用户.帐号和角色信息服务,并为Op ...
- saltstack之keepalived的安装配置
使用saltstack编译安装keepalived: 创建相应的目录,并在目录下创建相应的sls配置文件 [root@node1 ~]# mkdir /srv/salt/prod/keepalived ...
- chrome中,BackstopJS 使用cookie文件后依旧提示需要登录
1. 本地环境: apache-tomcat:7.05,jdk8, backstopJS:latest-version 配置tomcat管理用户成功:手工访问tomcat应用内部服务正常: 2. 问 ...