【关关的刷题日记63】Leetcode 111 Minimum Depth of Binary Tree

2017 年 12 月 11 日 专知 关关

点击上方“专知”,关注获取专业AI知识”

关关的刷题日记63 – Leetcode 111 Minimum Depth of Binary Tree

题目

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..

题目让我们求二叉树的最小深度:最小深度是指从根节点到最近叶子节点的最短路径的节点个数。

思路

思路:一颗二叉树的最小深度等于左子树的最小深度与右子树的最小深度的较小值加1,求左右子树的最小深度的做法同求这棵二叉树的最小深度做法一样,采用递归的解法。与求最大深度不同的是,当不存在左子树或者右子树的时候,这棵树的最小深度就是另外一个子树的最小深度。

class Solution {public:
int minDepth(TreeNode* root) {
if(root==nullptr)
return 0;
if(!root->left && !root->right)
return 1;
else if(root->left && !root->right)
return minDepth(root->left)+1;
else if(!root->left && root->right)
return minDepth(root->right)+1;
else return min(minDepth(root->left),minDepth(root->right))+1;
}};

人生易老,唯有陪伴最长情,加油!

以上就是关关关于这道题的总结经验,希望大家能够理解,有什么问题可以在我们的专知公众号平台上交流或者加我们的QQ专知-人工智能交流群 426491390,也可以加入专知——Leetcode刷题交流群(请先加微信小助手weixinhao: Rancho_Fang)。

专知网站查看Leetcode刷题日记:


请登录www.zhuanzhi.ai或者点击阅读原文,顶端搜索“Leetcode” 主题,取查看获得专知Leetcode所有资源!如下图所示~ 


 请感兴趣的同学,扫一扫下面群二维码,加入到专知-LeetCode学习交流群!(注明 Leetcode 刷题)




欢迎转发到你的微信群和朋友圈,分享专业AI知识!


获取更多关于机器学习以及人工智能知识资料,请访问www.zhuanzhi.ai,  或者点击阅读原文,即可得到!


-END-

欢迎使用专知

专知,一个新的认知方式!专注在人工智能领域为AI从业者提供专业可信的知识分发服务, 包括主题定制、主题链路、搜索发现等服务,帮你又好又快找到所需知识。


使用方法>>访问www.zhuanzhi.ai, 或点击文章下方“阅读原文”即可访问专知

中国科学院自动化研究所专知团队

@2017 专知

专 · 知


关注我们的公众号,获取最新关于专知以及人工智能的资讯、技术、算法、深度干货等内容。扫一扫下方关注我们的微信公众号。


点击“阅读原文”,使用专知

登录查看更多
6

相关内容

【天津大学】风格线条画生成技术综述
专知会员服务
31+阅读 · 2020年4月26日
【Google】监督对比学习,Supervised Contrastive Learning
专知会员服务
72+阅读 · 2020年4月24日
《动手学深度学习》(Dive into Deep Learning)PyTorch实现
专知会员服务
115+阅读 · 2019年12月31日
【强化学习资源集合】Awesome Reinforcement Learning
专知会员服务
93+阅读 · 2019年12月23日
【关关的刷题日记60】Leetcode 437. Path Sum III
【 关关的刷题日记53】 Leetcode 100. Same Tree
专知
10+阅读 · 2017年12月1日
【 关关的刷题日记47】Leetcode 38. Count and Say
【LeetCode 136】 关关的刷题日记32 Single Number
Implicit Maximum Likelihood Estimation
Arxiv
7+阅读 · 2018年9月24日
Arxiv
3+阅读 · 2018年8月17日
Two Stream 3D Semantic Scene Completion
Arxiv
4+阅读 · 2018年7月16日
W-net: Bridged U-net for 2D Medical Image Segmentation
Arxiv
19+阅读 · 2018年7月12日
VIP会员
Top
微信扫码咨询专知VIP会员