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相关推荐

  1. LeetCode: 111. Minimum Depth of Binary Tree

    题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bina ...

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

  3. leetcode python3 简单题111. Minimum Depth of Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十一题 (1)题目 英文: Given a binary tree, fin ...

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

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

  6. 111. Minimum Depth of Binary Tree 二叉树的最小深度

    给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], ...

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

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

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

  10. leetcode - Minimum Depth of Binary Tree

    题目:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is th ...

最新文章

  1. Spring Session官方介绍及spring框架学习方法
  2. Semaphore应用及原理
  3. python编写脚本方法_python 定时器,实现每天凌晨3点执行的方法
  4. sqlite查询乘以某列如果是null就换成_大数据之Hive group by with cube/rollup分组查询...
  5. 各家银行黄金T+D业务比较
  6. 一文带你从零认识什么是XLA
  7. Android 系统(247)---Android Go项目, Launcher no Notification Dots
  8. 分享多年收集的40款免费开源源码
  9. Jmeter IP欺骗
  10. Netty中ChannelOption属性含义
  11. 机器学习实战——决策树Python实现问题记录
  12. Vue项目上线(阿里云centos7+nginx)
  13. 2 会计要素和会计科目
  14. C++ 使用正则表达式
  15. 为远程群晖NAS配置稳定的公网地址 1/2
  16. windows 98 设置 TEMP 环境变量时的一个有趣现象
  17. 【学习记录】状语和状语从句
  18. 苹果微软小米华为,创新四重奏?
  19. 很实用的应用,推荐一下http://static.apk.hiapk.com/html/2012/06/625213.html?module=256info=MQA2ADMA
  20. 文本生成中的OOV问题

热门文章

  1. MySQL的条件判断函数
  2. windows 8授权概览
  3. 《易学Python》——第1章 为何学习Python 1.1 学习编程
  4. ember.js 101 入门教程 第一课
  5. RHEL6.5 部署DNS服务器
  6. Windows server 2008计划任务(批处理命令)不执行
  7. 一个老板向员工发的牢骚
  8. 04.spring boot配置文件--yml
  9. Why does getView return wrong convertView objects on BaseAdapter?
  10. 算法:数组找出2个只出现一次的数字(其他元素出现两次)