-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathwidthOfBinaryTree.java
57 lines (47 loc) · 1.54 KB
/
widthOfBinaryTree.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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
/*
use a travel traversal template, use another queue to keep the index of each level nodes. left node index = this node index * 2, right = this node index*2 + 1. the width should be the last node index - first node index + 1
*/
class Solution {
public int widthOfBinaryTree(TreeNode root) {
if(root == null) return 0;
Queue<TreeNode> queue = new ArrayDeque<>();
Queue<Integer> count = new ArrayDeque<>();
int max = 1;
queue.offer(root);
count.offer(1);
while(!queue.isEmpty())
{
int size = queue.size();
int left = 0;
int right = 0;
for(int i=0; i<size; i++)
{
TreeNode node = queue.poll();
int index = count.poll();
if(i==0) left = index;
if(i==size-1) right = index;
if(node.left != null)
{
queue.offer(node.left);
count.offer(index*2);
}
if(node.right != null)
{
queue.offer(node.right);
count.offer(index*2+1);
}
}
max = Math.max(max, right - left+1);
}
return max;
}
}