111. Minimum Depth of Binary Tree
来源:互联网 发布:mac 搭建gitlab服务器 编辑:程序博客网 时间:2024/06/01 07:34
问题描述;
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.
解题思路:
直接求最短路径就是左右子树depth相比较就行了。
class Solution {
public:
int minDepth(TreeNode* root) {
if(root==NULL) return 0;
else if(root->left==NULL&&root->right==NULL)
return 1;
else
{
int left_depth,right_depth;
if(root->left)
left_depth=minDepth(root->left);
else
left_depth=INT_MAX;
if(root->right)
right_depth=minDepth(root->right);
else
right_depth=INT_MAX;
return min(left_depth,right_depth)+1;
}
}
};
public:
int minDepth(TreeNode* root) {
if(root==NULL) return 0;
else if(root->left==NULL&&root->right==NULL)
return 1;
else
{
int left_depth,right_depth;
if(root->left)
left_depth=minDepth(root->left);
else
left_depth=INT_MAX;
if(root->right)
right_depth=minDepth(root->right);
else
right_depth=INT_MAX;
return min(left_depth,right_depth)+1;
}
}
};
阅读全文
0 0
- LeetCode 111. Minimum Depth of Binary Tree
- [LeetCode]111.Minimum Depth of Binary Tree
- 【LeetCode】111.Minimum Depth of Binary Tree
- [Leetcode] 111. Minimum Depth of Binary Tree
- 111.Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- [leetcode] 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree LeetCode
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 【LeetCode】111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- LeetCode *** 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- D
- CentOS使用yum update更新时不升级内核的方法
- Fedora18系统配置
- swap文件创建命令(简单记录)
- hyperledger fabric0.6 简析start
- 111. Minimum Depth of Binary Tree
- 分布式消息队列RocketMQ&Kafka -- 消息的“顺序消费”-- 一个看似简单的复杂问题
- 学习Java EE其它开发技术
- 【Json】JSONObejct格式转化问题
- 如何用Soft-NMS实现目标检测并提升准确率
- Easyui validator.js 表单日期框校验问题黑科技处理方式
- ssm框架 自动生成实体类、DAO接口和Mapping映射文件
- Mybatis代码生成工具
- C++类和对象