111. Minimum Depth of Binary Tree
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.
即这道题要求找出二叉树中根节点到最近一个叶节点的距离。
问题分析:
由于题目给出的树是一颗二叉树,那么整个问题就简单化了。我们只需要思考以下几种情况就好:①这个树为空,那么距离当然为0;②这个树只有一个根节点,那么距离为1;③这个树存在叶节点。第三种情况相对复杂一些,我们把情况细化,可以分解为,一个父节点到叶节点的最小距离,可以由其子节点到叶节点的最小距离得到。其中,父节点到叶节点的最小距离,为其左节点到叶节点的最小距离与其右节点到叶节点的最小距离直接的比较,取较小值加1.
那么,这道题可以用递归的方式求解。给出递归的终结条件(即该节点为空或者该节点为叶节点),按照第三种情况的关系实现即可。
代码:
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 #include <queue> 11 class Solution { 12 public: 13 int minDepth(TreeNode* root) { 14 if(!root) return 0; 15 if(!root->left && !root->right) return 1; 16 int left = minDepth(root->left); 17 int right = minDepth(root->right); 18 if(left == 0) 19 return right + 1; 20 if(right == 0) 21 return left + 1; 22 return left<right?left+1:right+1; 23 } 24 };
转载于:https://www.cnblogs.com/MT-ComputerVision/p/6985854.html
111. Minimum Depth of Binary Tree相关推荐
- LeetCode: 111. Minimum Depth of Binary Tree
题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bina ...
- 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 s ...
- leetcode python3 简单题111. Minimum Depth of Binary Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十一题 (1)题目 英文: Given a binary tree, fin ...
- 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 shor ...
- 111. Minimum Depth of Binary Tree
1.问题描述 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along t ...
- 111. Minimum Depth of Binary Tree 二叉树的最小深度
给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], ...
- 【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--Java, Python解法--二叉树最小高度--迭代,递归
题目地址:Minimum Depth of Binary Tree - LeetCode Given a binary tree, find its minimum depth. The minimu ...
- 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 - Minimum Depth of Binary Tree
题目:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is th ...
最新文章
- Spring Session官方介绍及spring框架学习方法
- Semaphore应用及原理
- python编写脚本方法_python 定时器,实现每天凌晨3点执行的方法
- sqlite查询乘以某列如果是null就换成_大数据之Hive group by with cube/rollup分组查询...
- 各家银行黄金T+D业务比较
- 一文带你从零认识什么是XLA
- Android 系统(247)---Android Go项目, Launcher no Notification Dots
- 分享多年收集的40款免费开源源码
- Jmeter IP欺骗
- Netty中ChannelOption属性含义
- 机器学习实战——决策树Python实现问题记录
- Vue项目上线(阿里云centos7+nginx)
- 2 会计要素和会计科目
- C++ 使用正则表达式
- 为远程群晖NAS配置稳定的公网地址 1/2
- windows 98 设置 TEMP 环境变量时的一个有趣现象
- 【学习记录】状语和状语从句
- 苹果微软小米华为,创新四重奏?
- 很实用的应用,推荐一下http://static.apk.hiapk.com/html/2012/06/625213.html?module=256info=MQA2ADMA
- 文本生成中的OOV问题