一、     称号

并寻求最深的二元相似。给定的二进制树。求其最小深度。

最小深度是沿从根节点,到叶节点最短的路径。

二、     分析

当我看到这个题目时。我直接将最深二叉树的代码略微改了下,把max改成min。本以为应该没有问题,谁知道WA了两次,我静下来看了看。最终知道了,当遇到有结点为NULL时就得要结束了。所下面次再简单的题目也要静下来好好分析,不然会easy出错。

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode *root) {
if(root==NULL)
return 0;
int mleft=minDepth(root->left);
int mright=minDepth(root->right);
if(mleft==0)
return 1+mright;
else if(mright==0)
return 1+mleft;
else return min(mleft,mright)+1;
}
}; 二、 * Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ class Solution {
public:
int minDepth(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
return minRec(root);
} int minRec( TreeNode * root) {
if(!root) return 0; int left = minRec( root->left);
int right = minRec( root->right); if(left && right) return 1 + min(left, right);
if(left || right) return 1+left+right;
return 1;
}
};
三、 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function return minRec(root);
} private int minRec(TreeNode root) {
if(root==null) return 0; int l = minRec(root.left);
int r = minRec(root.right); if(l==0) return r+1;
if(r==0) return l+1; return Math.min(l, r) + 1; }
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

Leetcode:minimum_depth_of_binary_tree解决问题的方法的更多相关文章

  1. 解决问题的方法=>现象-->原因-->方案-->方案的优缺点

    解决问题的方法=>现象-->原因-->方案-->方案的优缺点

  2. 访问 IIS 元数据库失败解决问题的方法

    近日调试一Asp.net程序,出现了“访问 IIS 元数据库失败”的错误信息,最后经过搜索发现了解决问题的方法. 解决方法如下: 1.依次点击“开始”-“运行”. 2.在“运行”栏内输入 “C:\WI ...

  3. Selenium私房菜系列10 -- 我遇到的问题及解决问题的方法

    Selenium私房菜系列10 -- 我遇到的问题及解决问题的方法

  4. Selenium私房菜系列9 -- 我遇到的问题及解决问题的方法

    Selenium私房菜系列10 -- 我遇到的问题及解决问题的方法

  5. leetcode解决问题的方法||Integer to Roman问题

    problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range ...

  6. 【LeetCode】【Python解决问题的方法】Best Time to Buy and Sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. [LeetCode] Decode Ways 解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  8. leetcode: sortlist之四种方法

    原题链接:https://oj.leetcode.com/problems/sort-list/ 题目:空间复杂度为常数,时间复杂度为O(nlogn)的排序链表实现 方法一:第一想法是模拟数组的快速排 ...

  9. NOIP 2012 解决问题的方法

    [D1T1vigenerepassword] P1778vigenerepassword Accepted 标签:[显示标签] 描写叙述 16世纪法国外交家Blaise de Vigenère设计了一 ...

随机推荐

  1. opencv 训练自己的分类器汇总

    原地址:http://www.cnblogs.com/zengqs/archive/2009/02/12/1389208.html OpenCV训练分类器 OpenCV训练分类器 一.简介 目标检测方 ...

  2. oracle事务(转)

    今天温习oracle事务,记录如下: 事务定义            事务是保持数据的一致性,它由相关的DDL或者DML语句做为载体,这组语句执行的结果要么一起成功,要么一起失败.        我们 ...

  3. c++ stl algorithm: std::fill, std::fill_n

    std::fill 在[first, last)范围内填充值 #include <iostream> #include <vector> #include <algori ...

  4. Indiegogo: An International Crowdfunding Platform to Raise Money

    Indiegogo: An International Crowdfunding Platform to Raise Money The world's funding platform. Fund ...

  5. ActionBar本部分适用述评

    http://note.youdao.com/share/?id=7f213cb64069bad221f4581507707294&type=note 因为把图片拿进来太麻烦,所以我给了一个直 ...

  6. http://www.shengshiyouxi.com

    android从Linux系统启动有4个步骤:   (1) init进程启动   (2) Native服务启动   (3) System Server,Android服务启动   (4) Home启动 ...

  7. hdu1081(最大子矩阵)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1081 分析:a[i][j]代表第i行,前j个数据的和:那么由a[i][j]可得sum[k][long] ...

  8. Software Development and Newton&#39;s Laws of Motion

    Software Development and Newton's Laws of Motion Intro I have no idea since when the word velocity f ...

  9. NYOJ 12 喷水装置(二)

    pid=12">喷水装置(二) 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n( ...

  10. SE 2014年5月9日

    两企业接入到 Internet(A公司和B公司),企业内部的用户及服务器均能够访问到 Internet. 2. A公司规模较大,采用了接入层/汇聚层/核心层的划分模式,接入层划分了多vLan(如图), ...