forked from kexun/sort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinTree.java
43 lines (38 loc) · 1002 Bytes
/
MinTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.demo;
/**
* 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.
* @author kexun
*
*/
public class MinTree {
public int run(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
if (root.left == null) {
return run(root.right)+1;
}
if (root.right == null) {
return run(root.left)+1;
}
return Math.min(run(root.left), run(root.right)) + 1;
}
class TreeNode {
public TreeNode left;
public TreeNode right;
}
public static void main(String[] args) {
TreeNode root = new MinTree(). new TreeNode();
root.left = new MinTree(). new TreeNode();
root.right = new MinTree(). new TreeNode();
MinTree t = new MinTree();
int i = t.run(root);
System.out.println(i);
}
}