Minimum Depth of Binary Tree(二叉树DFS)
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 binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
int mindepth;
public:
void tra(TreeNode* root,int depth){
if(root==NULL) return;
++depth;
if(!root->left&&!root->right&&depth!=&&depth<mindepth)//必须是叶节点才更新mindepth
mindepth=depth;
tra(root->left,depth);
tra(root->right,depth);
}
int minDepth(TreeNode *root) {
mindepth=;
if(!root) return ;
if(root->left==NULL&&root->right==NULL) return ;
tra(root,);
if(mindepth==){
return ;
}
return mindepth;
}
};
Minimum Depth of Binary Tree(二叉树DFS)的更多相关文章
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- [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 ...
- 【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 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- [Leetcode] The minimum depth of binary tree二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] 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 二叉树的最小深度
给定一个二叉树,找出其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数量.详见:https://leetcode.com/problems/minimum-depth-of-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] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
随机推荐
- JAVA300集笔记
章节2 java入门阶段 2.1注释 单行注释 // 多行注释 /* 内容*/ 文本注释/**内容*/ 注释是为了方便阅读代码,在编译时注释会被删除. 2.2 标识符 标识符作用: 标识符用来给变量 ...
- [BZOJ1088][SCOI2005]扫雷Mine DP
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1088 记录下每一个格子对应左边格子放的雷的情况,然后dp转移就好了. #include&l ...
- H.264学习笔记4——变换量化
A.变换量化过程总体介绍 经过帧内(16x16和4x4亮度.8x8色度)和帧间(4x4~16x16亮度.4x4~8x8色度)像素块预测之后,得到预测块的残差,为了压缩残差信息的统计冗余,需要对残差数据 ...
- 让TortoiseGit记住帐号密码方法
我的电脑环境是: Windows7 64x 系统用户名是:steden 所以,我的路径是:C:\Users\steden\ 具体要根据你的系统环境及当前用户名来决定. 在这里,有个文件:.gitc ...
- vue vueRouter vuex Axios webpack 前端常用内容
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中.
- java匹配http或https的url的正则表达式20180912
package org.jimmy.autosearch20180821.test; import java.util.regex.Matcher; import java.util.regex.Pa ...
- baidu让用户更快看到首页
//让用户更快看到首页 if(!location.hash.match(/[^a-zA-Z0-9]wd=/)) { document.getElementById("wrapper" ...
- 【牛客小白月赛6】 C 桃花 - 树上最长路
题目地址:https://www.nowcoder.com/acm/contest/136/C dfs找出最长路和次长路,将两个结果相加再加上起点即可: #include<iostream> ...
- 使用sphinx
SQL 结构化查询语言(是一种标准,所有的关系型数据库Mysql,sqlserver,oracle) sphinx的使用两种方式: 第一种: 使用sphinx的API来操作sphinx (常 ...
- double salary = wage = 9999.99错误
在看书时,有这么一句表达式 double salary = wage = 9999.99; 在Linux中编译时,不能通过,提示是 error: 'wage' was not declared in ...