|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import com.fishercoder.common.classes.TreeNode; |
| 4 | + |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +/** |
| 8 | + * 662. Maximum Width of Binary Tree |
| 9 | + * |
| 10 | + * Given a binary tree, write a function to get the maximum width of the given tree. |
| 11 | + * The width of a tree is the maximum width among all levels. |
| 12 | + * The binary tree has the same structure as a full binary tree, but some nodes are null. |
| 13 | + * The width of one level is defined as the length between the end-nodes |
| 14 | + * (the leftmost and right most non-null nodes in the level, |
| 15 | + * where the null nodes between the end-nodes are also counted into the length calculation. |
| 16 | +
|
| 17 | + Example 1: |
| 18 | + Input: |
| 19 | +
|
| 20 | + 1 |
| 21 | + / \ |
| 22 | + 3 2 |
| 23 | + / \ \ |
| 24 | + 5 3 9 |
| 25 | +
|
| 26 | + Output: 4 |
| 27 | + Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9). |
| 28 | +
|
| 29 | + Example 2: |
| 30 | + Input: |
| 31 | +
|
| 32 | + 1 |
| 33 | + / |
| 34 | + 3 |
| 35 | + / \ |
| 36 | + 5 3 |
| 37 | +
|
| 38 | + Output: 2 |
| 39 | + Explanation: The maximum width existing in the third level with the length 2 (5,3). |
| 40 | +
|
| 41 | + Example 3: |
| 42 | + Input: |
| 43 | +
|
| 44 | + 1 |
| 45 | + / \ |
| 46 | + 3 2 |
| 47 | + / |
| 48 | + 5 |
| 49 | +
|
| 50 | + Output: 2 |
| 51 | + Explanation: The maximum width existing in the second level with the length 2 (3,2). |
| 52 | + Example 4: |
| 53 | + Input: |
| 54 | +
|
| 55 | + 1 |
| 56 | + / \ |
| 57 | + 3 2 |
| 58 | + / \ |
| 59 | + 5 9 |
| 60 | + / \ |
| 61 | + 6 7 |
| 62 | +
|
| 63 | + Output: 8 |
| 64 | + Explanation:The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7). |
| 65 | +
|
| 66 | + Note: Answer will in the range of 32-bit signed integer. |
| 67 | + */ |
| 68 | +public class _662 { |
| 69 | + public int widthOfBinaryTree(TreeNode root) { |
| 70 | + if (root == null) { |
| 71 | + return 0; |
| 72 | + } |
| 73 | + Queue<Map.Entry<TreeNode, Integer>> queue = new LinkedList<>(); |
| 74 | + queue.offer(new AbstractMap.SimpleEntry<>(root, 1)); |
| 75 | + int max = 1; |
| 76 | + while (!queue.isEmpty()) { |
| 77 | + int size = queue.size(); |
| 78 | + List<Map.Entry<TreeNode, Integer>> list = new ArrayList<>(); |
| 79 | + for (int i = 0; i < size; i++) { |
| 80 | + Map.Entry<TreeNode, Integer> curr = queue.poll(); |
| 81 | + if (curr.getKey().left != null) { |
| 82 | + Map.Entry<TreeNode, Integer> newEntry = new AbstractMap.SimpleEntry<>(curr.getKey().left, curr.getValue() * 2 - 1); |
| 83 | + queue.offer(newEntry); |
| 84 | + list.add(newEntry); |
| 85 | + } |
| 86 | + if (curr.getKey().right != null) { |
| 87 | + Map.Entry<TreeNode, Integer> newEntry = new AbstractMap.SimpleEntry<>(curr.getKey().right, curr.getValue() * 2); |
| 88 | + queue.offer(newEntry); |
| 89 | + list.add(newEntry); |
| 90 | + } |
| 91 | + } |
| 92 | + if (list.size() > 1) { |
| 93 | + max = Math.max(list.get(list.size() - 1).getValue() - list.get(0).getValue() + 1, max); |
| 94 | + } |
| 95 | + } |
| 96 | + return max; |
| 97 | + } |
| 98 | +} |
0 commit comments