LeetCode_111. Minimum Depth of Binary Tree
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.
package leetcode.easy; /**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode(int x) { val = x; } }
*/
public class MinimumDepthOfBinaryTree {
public int minDepth(TreeNode root) {
if (null == root) {
return 0;
} else if (null == root.left) {
return 1 + minDepth(root.right);
} else if (null == root.right) {
return 1 + minDepth(root.left);
} else {
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
}
} @org.junit.Test
public void test() {
TreeNode tn11 = new TreeNode(3);
TreeNode tn21 = new TreeNode(9);
TreeNode tn22 = new TreeNode(20);
TreeNode tn33 = new TreeNode(15);
TreeNode tn34 = new TreeNode(7);
tn11.left = tn21;
tn11.right = tn22;
tn21.left = null;
tn21.right = null;
tn22.left = tn33;
tn22.right = tn34;
tn33.left = null;
tn33.right = null;
tn34.left = null;
tn34.right = null;
System.out.println(minDepth(tn11));
}
}
LeetCode_111. Minimum Depth of Binary Tree的更多相关文章
- [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 ...
- 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 ...
- 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...
- 【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】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- LeetCode My Solution: Minimum Depth of Binary Tree
Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- 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 ...
随机推荐
- 在vue中使用css预编辑器
vue中使用less 安装less依赖,npm install less less-loader --save vue中使用sass npm install --save-dev sass-loade ...
- MLP多层感知机
@author:wepon @blog:http://blog.csdn.net/u012162613/article/details/43221829 转载:http://blog.csdn.net ...
- vue:概要
一.环境 #安装nodejs-官网安装包配置环境变量 node -v #安装webpack npm install webpack -g #安装vue-cli npm install vue-cli ...
- 基础 | BIO、NIO与AIO
本文链接:https://blog.csdn.net/bingbeichen/article/details/83617163 Java中的IO部分比较复杂,具体可参看书籍<Java NIO&g ...
- 解决 ImportError: No module named 'pip._internal'问题
pip错误 ImportError: No module named 'pip_internal' 解决 ImportError: No module named 'pip._internal' 问 ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- Bzoj 3123: [Sdoi2013]森林(主席树+启发式合并)
3123: [Sdoi2013]森林 Time Limit: 20 Sec Memory Limit: 512 MB Description Input 第一行包含一个正整数testcase,表示当前 ...
- Git 相关使用
https://www.cnblogs.com/mengdd/p/3447464.html 删除本地 & 远程 的分支. 删除本地分支 命令行 : $ git branch -d < ...
- 1059 Prime Factors(25 分)
Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...
- ST表(模板)「 查询区间最值 」
The Water Problem HDU - 5443 「 第一部分nlogn预处理 第二部分O(1)询问 」 #include <iostream> #include <bi ...