Merge pull request #602 from hailincai/master

Update 0102.二叉树的层序遍历.md
This commit is contained in:
程序员Carl
2021-08-16 09:46:51 +08:00
committed by GitHub

View File

@ -1205,6 +1205,35 @@ public:
};
```
java代码
```java
class Solution {
public Node connect(Node root) {
Queue<Node> tmpQueue = new LinkedList<Node>();
if (root != null) tmpQueue.add(root);
while (tmpQueue.size() != 0){
int size = tmpQueue.size();
Node cur = tmpQueue.poll();
if (cur.left != null) tmpQueue.add(cur.left);
if (cur.right != null) tmpQueue.add(cur.right);
for (int index = 1; index < size; index++){
Node next = tmpQueue.poll();
if (next.left != null) tmpQueue.add(next.left);
if (next.right != null) tmpQueue.add(next.right);
cur.next = next;
cur = next;
}
}
return root;
}
}
```
python代码