Skip to content

Commit 018f960

Browse files
authored
Create 111. Minimum Depth of Binary Tree
1 parent 9db27a9 commit 018f960

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

111. Minimum Depth of Binary Tree

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/****
2+
Given a binary tree, find its minimum depth.
3+
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
4+
****/
5+
6+
//Java solution:
7+
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode(int x) { val = x; }
15+
* }
16+
*/
17+
public class Solution {
18+
public int minDepth(TreeNode root) {
19+
if(root == null){
20+
return 0;
21+
}
22+
23+
if(root.left == null || root.right == null){
24+
return Math.max(minDepth(root.left), minDepth(root.right))+1;
25+
}
26+
27+
return Math.min(minDepth(root.left), minDepth(root.right))+1;
28+
}
29+
}

0 commit comments

Comments
 (0)