mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update 0102.二叉树的层序遍历.md
Add Java implementation for leetcode 116
This commit is contained in:
@ -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代码:
|
||||
|
||||
|
Reference in New Issue
Block a user