Update 0102.二叉树的层序遍历.md

Add Java implementation for leetcode 116
This commit is contained in:
hailincai
2021-08-13 14:57:54 -04:00
committed by GitHub
parent 70524a1521
commit a395f6f9df

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代码