Skip to content

Commit e17c379

Browse files
committed
fd
1 parent 463174d commit e17c379

File tree

3 files changed

+32
-51
lines changed

3 files changed

+32
-51
lines changed

leetcode/solution/src/BinaryTreeInorderTraversal.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import java.util.List;
44
import java.util.Stack;
55

6+
/**
7+
* https://leetcode.com/articles/binary-tree-inorder-traversal/
8+
*/
9+
610
public class BinaryTreeInorderTraversal {
711

812
public List<Integer> inorderTraversal(TreeNode root) {

leetcode/solution/src/MinimumDepthOfBinaryTree.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,15 @@ public int minDepth(TreeNode root) {
1616
if (root == null) {
1717
return 0;
1818
}
19-
// 如果一边空了,那这边深度为0,但是由于不是叶子节点,所以不能算,得看另一边。
19+
if (root.left == null && root.right == null) {
20+
return 1;
21+
}
2022
if (root.left == null) {
2123
return minDepth(root.right) + 1;
22-
} else if (root.right == null) {
23-
return minDepth(root.left) + 1;
24-
} else {
25-
return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
2624
}
27-
}
28-
29-
/**
30-
public int minDepth(TreeNode root) {
31-
if (root == null) {
32-
return 0;
25+
if (root.right == null) {
26+
return minDepth(root.left) + 1;
3327
}
34-
return helper(root);
28+
return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
3529
}
36-
37-
private int helper(TreeNode root) {
38-
if (root == null) {
39-
return Integer.MAX_VALUE;
40-
}
41-
if (root.left == null && root.right == null) {
42-
return 1;
43-
}
44-
return Math.min(helper(root.left), helper(root.right)) + 1;
45-
}*/
4630
}

leetcode/src/Main.java

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,33 @@
44

55
public class Main {
66

7-
public List<TreeNode> generateTrees(int n) {
8-
if (n == 0) {
9-
return Collections.EMPTY_LIST;
7+
public int minDepth(TreeNode root) {
8+
if (root == null) {
9+
return 0;
1010
}
11-
12-
List<TreeNode>[] map = new LinkedList[n + 1];
13-
map[0] = new LinkedList<>();
14-
map[0].add(null);
15-
16-
for (int i = 1; i <= n; i++) {
17-
map[i] = new LinkedList<>();
18-
for (int j = 1; j <= i; j++) {
19-
for (TreeNode left : map[j - 1]) {
20-
for (TreeNode right : map[i - j]) {
21-
TreeNode root = new TreeNode(j);
22-
root.left = left;
23-
root.right = clone(right, j);
24-
map[i].add(root);
25-
}
26-
}
27-
}
11+
if (root.left == null && root.right == null) {
12+
return 1;
2813
}
29-
30-
return map[n];
14+
if (root.left == null) {
15+
return minDepth(root.right) + 1;
16+
}
17+
if (root.right == null) {
18+
return minDepth(root.left) + 1;
19+
}
20+
return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
3121
}
3222

33-
public TreeNode clone(TreeNode node, int offset) {
34-
if (node == null) {
35-
return null;
23+
public int minDepth(TreeNode root, int depth) {
24+
if (root.left == null && root.right == null) {
25+
return depth;
26+
}
27+
if (root.left == null) {
28+
return minDepth(root.right, depth + 1);
29+
}
30+
if (root.right == null) {
31+
return minDepth(root.left, depth + 1);
3632
}
37-
TreeNode root = new TreeNode(node.val + offset);
38-
root.left = clone(node.left, offset);
39-
root.right = clone(node.right, offset);
40-
return root;
33+
return Math.min(minDepth(root.left, depth + 1), minDepth(root.right, depth + 1));
4134
}
4235

4336
public static void main(String[] args) {

0 commit comments

Comments
 (0)