- 方式一
利用hashmap记录每一个节点所在的层,利用队列进行宽度遍历
hashmap
private int getMaxWidth1(Node head){
int curMax = 0;
int curLevel = 1;
int curWidth = 0;
HashMap<Node, Integer> map = new HashMap<>();
map.put(head,1);
LinkedList<Node> queue = new LinkedList<>();
queue.add(head);
while (!queue.isEmpty()){
Node node = queue.poll();
if (null != node.left){
queue.add(node.left);
map.put(node.left, map.get(node)+1);
}
if (null != node.right){
queue.add(node.right);
map.put(node.right, map.get(node)+1);
}
if (map.get(node)>curLevel){
curWidth = 1;
curLevel = map.get(node);
} else {
curWidth++;
}
curMax = Math.max(curMax, curWidth);
}
return curMax;
}
- 方式二
利用队列进行宽度遍历,同时记录当前行的最后一个结点,以及下一行的最后一个结点,用于控制结束
不用hashmap
private static int getMaxWidth2(Node head){
Node nextEnd = null;
int curNodeCount = 0;
int maxWidth = 0;
LinkedList<Node> queue = new LinkedList<>();
queue.add(head);
Node curEnd = head;
while (!queue.isEmpty()){
Node node = queue.poll();
if (null != node.left){
queue.add(node.left);
nextEnd = node.left;
}
if (null != node.right){
queue.add(node.right);
nextEnd = node.right;
}
curNodeCount++;
if (node == curEnd){
maxWidth = Math.max(maxWidth, curNodeCount);
System.out.println(curNodeCount + "===" + maxWidth);
curEnd = nextEnd;
nextEnd = null;
curNodeCount= 0;
}
}
return maxWidth;
}
完整代码
public class TreeMaxWidth {
public static class Node{
private int value;
private Node left;
private Node right;
public Node(int value) {
this.value = value;
}
}
private static int getMaxWidth(Node head){
int curMax = 0;
int curLevel = 1;
int curWidth = 0;
HashMap<Node, Integer> map = new HashMap<>();
map.put(head,1);
LinkedList<Node> queue = new LinkedList<>();
queue.add(head);
while (!queue.isEmpty()){
Node node = queue.poll();
if (null != node.left){
queue.add(node.left);
map.put(node.left, map.get(node)+1);
}
if (null != node.right){
queue.add(node.right);
map.put(node.right, map.get(node)+1);
}
if (map.get(node)>curLevel){
curWidth = 1;
curLevel = map.get(node);
} else {
curWidth++;
}
curMax = Math.max(curMax, curWidth);
}
return curMax;
}
private static int getMaxWidth2(Node head){
Node nextEnd = null;
int curNodeCount = 0;
int maxWidth = 0;
LinkedList<Node> queue = new LinkedList<>();
queue.add(head);
Node curEnd = head;
while (!queue.isEmpty()){
Node node = queue.poll();
if (null != node.left){
queue.add(node.left);
nextEnd = node.left;
}
if (null != node.right){
queue.add(node.right);
nextEnd = node.right;
}
curNodeCount++;
if (node == curEnd){
maxWidth = Math.max(maxWidth, curNodeCount);
System.out.println(curNodeCount + "===" + maxWidth);
curEnd = nextEnd;
nextEnd = null;
curNodeCount= 0;
}
}
return maxWidth;
}
public static void main(String[] args) {
Node node = new Node(1);
node.left = new Node(2);
node.right = new Node(3);
node.left.left = new Node(4);
node.left.right = new Node(5);
node.right.left = new Node(6);
node.right.right = new Node(7);
node.left.left.left = new Node(8);
node.left.left.right = new Node(9);
node.left.right.left = new Node(10);
// int maxWidth = getMaxWidth(node);
// System.out.println(maxWidth);
int maxWidth2 = getMaxWidth2(node);
System.out.println(maxWidth2);
}
}

792

被折叠的 条评论
为什么被折叠?



