-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path662.maximum-width-of-binary-tree.java
67 lines (65 loc) · 1.86 KB
/
662.maximum-width-of-binary-tree.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
57
58
59
60
61
62
63
64
65
66
/*
* @lc app=leetcode id=662 lang=java
*
* [662] Maximum Width of Binary Tree
*/
// @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 {
/*
1(0)
/ \
3(0) 2(1)
/ \ \
5(0) 3(1) (2) 9(3)
对于某一个depth, 该行最多2 * depth - 1个node
如果某一个node的index是i,其left&right child的index是2 * i, 2 * i + 1
BFS
利用一个map存储对应node和index和关系,如果下一层有节点,就存进对应的index,并记录每层的length, 找到最大值
time: O(n)
space: O(n)
*/
public int widthOfBinaryTree(TreeNode root) {
if (root == null) return 0;
int res = 0;
Map<TreeNode, Integer> map = new HashMap<>();
map.put(root, 0);
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
while(!q.isEmpty()) {
int size = q.size();
int start = 0, end = 0;
for (int i = 0; i < size; ++i) {
TreeNode cur = q.poll();
int pos = map.get(cur);
if (i == 0) start = pos;
if (i == size - 1) end = pos;
if (cur.left != null) {
map.put(cur.left, 2 * pos);
q.offer(cur.left);
}
if (cur.right != null) {
map.put(cur.right, 2 * pos + 1);
q.offer(cur.right);
}
}
res = Math.max(res, end - start + 1);
}
return res;
}
}
// @lc code=end