【Minimum Depth of Binary Tree】cpp
题目:
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.
代码:
/**
* Definition for a binary tree node.
* 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) return ;
if ( !root->left && !root->right) return ;
if ( root->left && root->right ) return std::min( Solution::minDepth(root->left)+, Solution::minDepth(root->right)+);
if ( root->left ) return Solution::minDepth(root->left)+;
if (root->right ) return Solution::minDepth(root->right)+;
}
};
tips:
深搜思路(递归实现)。这里需要控制向下进行的条件。
1. 如果root是NULL,返回0
2. 如果root不是NULL,且left和right都是NULL,则到达叶子节点返回1(代表算上叶子节点的那一层)
3. 如果root->left和root->right都不为NULL,则继续往两边深搜
4. 如果root不是NULL,但root->left或root->right哪一方为NULL,则为NULL的一端不会再有叶子节点出现,不能再往下走了。
完毕。
============================================
第二次过这道题,上来没有把终止条件想完全。终止条件应该是达到叶子简单root->left root->right都为空。
修改了一次后AC了。
/**
* Definition for a binary tree node.
* 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 ) return ;
int min_depth = INT_MAX;
Solution::depth(root, min_depth, );
return min_depth;
}
static void depth(TreeNode* root, int& min_depth, int dep)
{
if ( !root->left && !root->right ) min_depth = min(min_depth,dep);
if ( root->left ) Solution::depth(root->left, min_depth, dep+);
if ( root->right ) Solution::depth(root->right, min_depth, dep+);
}
};
【Minimum Depth of Binary Tree】cpp的更多相关文章
- 【二叉树的递归】01二叉树的最小深度【Minimum Depth of Binary Tree】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,找出他的最小的深度 ...
- 【Maximum Depth of Binary Tree 】cpp
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- 【二叉树的递归】02二叉树的最大深度【Maximum Depth of Binary Tree】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,找出他的最小的深度 ...
- 【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练习题】Minimum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
[104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary t ...
- 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][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
随机推荐
- 一步一步学习SignalR进行实时通信_1_简单介绍
一步一步学习SignalR进行实时通信\_1_简单介绍 SignalR 一步一步学习SignalR进行实时通信_1_简单介绍 前言 SignalR介绍 支持的平台 相关说明 OWIN 结束语 参考文献 ...
- s3c6410_u-boot-2010.03移植【续】
本文接上一篇:http://www.cnblogs.com/tanghuimin0713/p/3965528.html 6.3)重新编译,烧写,运行 U-Boot - ::) for SMDK6410 ...
- linux系统下将php和mysql命令加入到环境变量中的方法
在Linux CentOS系统上安装完php和MySQL后,为了使用方便,需要将php和mysql命令加到系统命令中,如果在没有添加到环境变量之前,执行 “php -v”命令查看当前php版本信息时时 ...
- ARC————自动引用计数
一.内存管理/引用计数 1.引用计数式内存管理的方式(下面四种) 对象操作 OC方法 生成并持有对象 alloc/new/copy/mutableCopyd等方法 持有对象 retain方法 释放对象 ...
- HTTP协议中PUT/GET/POST/HEAD等介绍
HTTP协议中GET.POST和HEAD的介绍 GET: 请求指定的页面信息,并返回实体主体. HEAD: 只请求页面的首部. POST: 请求服务器接受所指定的文档作为对所标识的URI的新的从属实体 ...
- 状态可以通过动画切换的按钮--第三方开源--TickPlusDrawable
Android tickplusdrawable(TickPlusDrawable)在github上的项目主页是:https://github.com/flavienlaurent/tickplusd ...
- ViewPage显示Fragment集合实现左右滑动并且出现tab栏--第三方开源--SlidingTabLayout和SlidingTabStrip实现
注意:有关Fragment的方法和ViewPager的全部是android.support.v4包的,否则会报很多的错误 MainActivity: package com.zzw.fragmentt ...
- 批量删除的js代码
<script type="text/javascript"> function seltAll(){ var chckBoxSign = document.getEl ...
- IOS学习4
---恢复内容开始--- UIScrollView 屏幕展示有限,超出一个屏时用户可滚动查看过多部分.UIView不具备滚动功能. -取消autolayout -设置CGSize contentSiz ...
- javascript中split字符串分割函数
1. var ss=s.split("fs"); for(var i=0;i<ss.length;i++){ 处理每一个ss[i]; } 2. "2:3:4:5&q ...