标签:connect i++ for style pop 注意 ++ poll solution
1 class Solution { 2 public void connect(TreeLinkNode root) { 3 if(root == null) return; 4 Queue<TreeLinkNode> queue = new LinkedList<>(); 5 queue.offer(root); 6 int size = queue.size(); 7 while(!queue.isEmpty()) { 8 TreeLinkNode prev = null; 9 for(int i = 0; i < size; i++) { 10 TreeLinkNode node = queue.poll(); 11 if(node.left != null) { //注意不是空的再加进去 别忘了 12 queue.offer(node.left); 13 } 14 if(node.right != null) { 15 queue.offer(node.right); 16 } 17 if(i == 0) { 18 prev = node; 19 }else{ 20 prev.next = node; 21 prev = node; 22 } 23 if(i == size - 1) { 24 node.next = null; 25 } 26 } 27 size = queue.size(); 28 } 29 } 30 }
116. Populating Next Right Pointers in Each Node
标签:connect i++ for style pop 注意 ++ poll solution
原文地址:https://www.cnblogs.com/goPanama/p/9689045.html