-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path111.二叉树的最小深度.java
57 lines (52 loc) · 1.39 KB
/
111.二叉树的最小深度.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
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.Deque;
import java.util.LinkedList;
import javax.management.relation.Role;
import javax.swing.tree.TreeNode;
/*
* @lc app=leetcode.cn id=111 lang=java
*
* [111] 二叉树的最小深度
*/
// @lc code=start
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
Deque<TreeNode> deque = new LinkedList<>();
deque.offer(root);
int level = 1;
while (!deque.isEmpty()) {
int size = deque.size();
for (int i = 0; i < size; i++) {
TreeNode node = deque.poll();
if (node.left == null && node.right == null) {
return level;
}
// only insert node that isn't null
if (node.left != null) {
deque.offer(node.left);
}
if (node.right != null) {
deque.offer(node.right);
}
}
level++;
}
return level;
}
}
// @lc code=end