(二叉树 BFS DFS) 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 shortest path from the root node down to the nearest leaf node.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7]
,
3
/ \
9 20
/ \
15 7
return its minimum depth = 2.
------------------------------------------------------------------------------------------------------------------------------
求二叉树的最小深度,嗯,这个题可以用DFS,也可以用BFS,我这个题先用BFS写,然后用DFS。
要注意,注意,这个最小深度指的是,从根节点到最近的叶子节点的距离,比如[1,1]这个最小深度就是2!!!,同样,最大深度也是2。
[1,1]图:
所以在用BFS时,只有这个结点的左子节点和右子节点同时为NULL时,才能确定为叶子节点,才能返回sum(表示最小深度)。
同理,在用DFS时,当左子节点为NULL,而右子节点不为空,那就不是叶子节点,还得计算。同理,如果右子节点为NULL,而左子节点也不是,就得继续递归下去。
C++代码:
/**
* 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) {
queue<TreeNode*> q;
if(!root)
return ;
q.push(root);
int sum = ;
while(!q.empty()){
sum++;
for(int i = q.size(); i > ; i--){ //必须写循环,如果不写,对于[1,2,3,4,5],将会返回3,与题目要求不符。
auto t = q.front();
q.pop();
if(!t->left &&!t->right) return sum;
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
}
return ;
}
};
DFS:
C++代码:
/**
* 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) return + minDepth(root->right); //这个要注意。
if(!root->right) return + minDepth(root->left); //这个要注意。
return min( + minDepth(root->left), + minDepth(root->right));
}
};
(二叉树 BFS DFS) leetcode 111. Minimum Depth of Binary Tree的更多相关文章
- (二叉树 BFS DFS) leetcode 104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [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] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- 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 111 Minimum Depth of Binary Tree(DFS)
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
problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...
- leetcode 111 Minimum Depth of Binary Tree ----- java
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- Java [Leetcode 111]Minimum Depth of Binary Tree
题目描述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...
随机推荐
- 三、K8S成功
kubeadm join 172.17.149.114:6443 --token yjogpa.kk85u2i4n5rt1omq --discovery-token-ca-cert-hash sha2 ...
- CS新建排版
1.拉菜单栏barmanage,去掉不要的头部和尾部 ,选择控件bar属性optionsbar 全部为false,防止菜单拖动. 2.拉一个panelcontrol属性dock 设置顶部,在拉一个p ...
- Multi-Targeting and Porting a .NET Library to .NET Core 2.0
Creating a new .NET Standard Project The first step for moving this library is to create a new .NET ...
- vuex2.0 基本使用(1) --- state
Vuex 的核心是 store, 它是一个通过 Vuex.Store 构造函数生成的对象.为什么它会是核心呢?因为我们调用这个构造函数创建store 对象的时候,给它传递参数中包装了state, mu ...
- Nginx geoip模块
需要编译进 --with-http_geoip_module 首先要安装maxMind里的geoip的c开发库 https://dev.maxmind.com/geoip/legacy/downloa ...
- VMware ezmomi工具使用
用两个静态IP克隆模板: ezmomi clone --template centos67 --hostname test01 --cpus 2 --mem 4 --ips 172.10.16.203 ...
- Codeforces Round #449 Div. 1
B:注意到nc/2<=m,于是以c/2为界决定数放在左边还是右边,保证序列满足性质的前提下替换掉一个数使得其更靠近边界即可. #include<iostream> #include& ...
- Android ProgressDialog 简单实用
ProgressDialog progressDialog; @SuppressLint("HandlerLeak") Handler handler1 = new Handler ...
- github Permission denied (publickey). fatal: Could not read from remote repository.
github Permission denied (publickey).fatal: Could not read from remote repository. ----------------- ...
- 【AtCoder - 2300】Snuke Line(树状数组)
BUPT2017 wintertraining(15) #9A 题意 有n个纪念品,购买区间是\([l_i,r_i]\).求每i(1-m)站停一次,可以买到多少纪念品. 题解 每隔d站停一次的列车,一 ...