Skip to content

Commit a395f6f

Browse files
authored
Update 0102.二叉树的层序遍历.md
Add Java implementation for leetcode 116
1 parent 70524a1 commit a395f6f

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

problems/0102.二叉树的层序遍历.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,35 @@ public:
12051205
};
12061206
```
12071207

1208+
java代码:
1209+
1210+
```java
1211+
class Solution {
1212+
public Node connect(Node root) {
1213+
Queue<Node> tmpQueue = new LinkedList<Node>();
1214+
if (root != null) tmpQueue.add(root);
1215+
1216+
while (tmpQueue.size() != 0){
1217+
int size = tmpQueue.size();
1218+
1219+
Node cur = tmpQueue.poll();
1220+
if (cur.left != null) tmpQueue.add(cur.left);
1221+
if (cur.right != null) tmpQueue.add(cur.right);
1222+
1223+
for (int index = 1; index < size; index++){
1224+
Node next = tmpQueue.poll();
1225+
if (next.left != null) tmpQueue.add(next.left);
1226+
if (next.right != null) tmpQueue.add(next.right);
1227+
1228+
cur.next = next;
1229+
cur = next;
1230+
}
1231+
}
1232+
1233+
return root;
1234+
}
1235+
}
1236+
```
12081237

12091238
python代码:
12101239

0 commit comments

Comments
 (0)