java求二叉树的最大宽度
时间: 2023-11-29 17:46:41 浏览: 76
假设二叉树采用链接存储结构存储,设计一个算法,求二叉树b的宽度.pdf
以下是Java求二叉树最大宽度的代码实现:
```java
import java.util.LinkedList;import java.util.Queue;
public class Solution {
public int widthOfBinaryTree(TreeNode root) {
if (root == null) {
return 0;
}
Queue<TreeNode> queue = new LinkedList<>();
Queue<Integer> indexQueue = new LinkedList<>();
queue.offer(root);
indexQueue.offer(1);
int maxWidth = 0;
while (!queue.isEmpty()) {
int size = queue.size();
int left = 0, right = 0;
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
int index = indexQueue.poll();
if (i == 0) {
left = index;
}
if (i == size - 1) {
right = index;
}
if (node.left != null) {
queue.offer(node.left);
indexQueue.offer(index * 2);
}
if (node.right != null) {
queue.offer(node.right);
indexQueue.offer(index * 2 + 1);
}
}
maxWidth = Math.max(maxWidth, right - left + 1);
}
return maxWidth;
}
}
```
阅读全文