diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 007eac44..0f9b2df6 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1205,6 +1205,35 @@ public: }; ``` +java代码: + +```java +class Solution { + public Node connect(Node root) { + Queue tmpQueue = new LinkedList(); + 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代码: