LeetCode-MinimumDepthOfBinaryTree
题目:
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
分析:
查找二叉树的最小深度
分2种情况:
如果结点为NULL,返回0
否则,返回1+左右结点的最小深度
但这里有个地方需要注意,如果左右结点有一个为空,则应该返回1+另一个不为空的深度
如果左右节点都为空直接返回1
总结一下就是
if(l==0 || r==0)
return 1+l+r;
AC代码
class Solution {
public:
int minDepth(TreeNode* root) {
if(!root)
return 0;
int l = minDepth(root->left);
int r = minDepth(root->right);
if(l==0 || r==0)
return 1+l+r;
return 1+min(l,r);
}
};
LeetCode-MinimumDepthOfBinaryTree的更多相关文章
- leetcode — minimum-depth-of-binary-tree
/** * Source : https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ * * * Given a binary t ...
- [牛客网 -leetcode在线编程 -02] minimum-depth-of-binary-tree -树的最短深度
题目描述 题目描述 Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along ...
- minimum-depth-of-binary-tree leetcode C++
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the short ...
- [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算法分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- 【LeetCode OJ】Minimum Depth of Binary Tree
Problem Link: http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ To find the minimum dept ...
- LeetCode题目分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- [LeetCode]题解(python):111 Minimum Depth of Binary Tree
题目来源 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minim ...
- <转>LeetCode 题目总结/分类
原链接:http://blog.csdn.net/yangliuy/article/details/44514495 注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http:/ ...
- Minimum Depth of Binary Tree ——LeetCode
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
随机推荐
- linux下php的一些问题
一.Loaded Configuration File none Configuration File (php.ini) Path /data/service/php/etc 在etc目录下有ph ...
- Doxygen生成C++中文文档配置注意事项
打开对应的Doxyfile,修改如下: 1.OUTPUT_LANGUAGE = Chinese. 2.INPUT_ENCODING = GB2312.
- STS没有找到Dynamic Web Project
解决:安装JavaEE插件 help-> install new software-> 选择sts对应的eclipse版本站点,如eclipse版本4.09选择2018-09.4.10选择 ...
- wpgcms---设置应用模板
wpgcms设置应用模板,找了好半天才找到. 第一步:找到对应的应用(例如:案例) 选择“界面”,前台页面 设置列表模板: 设置详情模板:
- python---使用md5加密
python中使用md5进行加密字符串: __author__ = 'Administrator' #-*- coding: utf-8 -*- import hashlib aa = ' #需要加密 ...
- centos 安装laravel
1.下载composer并全局安装 curl -sS https://getcomposer.org/installer | php 2.查看全局命令目录 echo $PATH 移动composer到 ...
- ABP之应用服务(2)
在上一篇的笔记中,已经大致对Application层的使用作了简要的使用说明,感觉还是有些东西需要研究一下,所以承接上文,对AutoMapper这个方便的东西,稍微研究一下. 一.初识AutoMapp ...
- 把Asp.Net Core 2.0部署在Linux上,使用Nginx代理服务器,并且用Systemctl命令以服务的方式监听项目
在Linux上部署.net core 2.0程序: 第一步:配置Nginx代理 在/etc/nginx/sites-available/default 中添加 server { listen ; lo ...
- HDU 6008 - Worried School
Worried School Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 批量转换gbk编码的java代码为utf8
#!/bin/bash echo $1 echo $# if [ ! $# -eq 1 ]; then echo "usage: ./gbk2utf8.sh src" ...