Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 71865c1

Browse files
Merge pull request youngyangyang04#1351 from wzqwtt/tree02
添加(二叉树的统一迭代法、102.二叉树的层序遍历、107.二叉树的层次遍历II)Scala版本
2 parents da137a1 + edbb487 commit 71865c1

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,31 @@ func levelOrder(_ root: TreeNode?) -> [[Int]] {
318318
return result
319319
}
320320
```
321+
Scala:
322+
```scala
323+
// 102.二叉树的层序遍历
324+
object Solution {
325+
import scala.collection.mutable
326+
def levelOrder(root: TreeNode): List[List[Int]] = {
327+
val res = mutable.ListBuffer[List[Int]]()
328+
if (root == null) return res.toList
329+
val queue = mutable.Queue[TreeNode]() // 声明一个队列
330+
queue.enqueue(root) // 把根节点加入queue
331+
while (!queue.isEmpty) {
332+
val tmp = mutable.ListBuffer[Int]()
333+
val len = queue.size // 求出len的长度
334+
for (i <- 0 until len) { // 从0到当前队列长度的所有节点都加入到结果集
335+
val curNode = queue.dequeue()
336+
tmp.append(curNode.value)
337+
if (curNode.left != null) queue.enqueue(curNode.left)
338+
if (curNode.right != null) queue.enqueue(curNode.right)
339+
}
340+
res.append(tmp.toList)
341+
}
342+
res.toList
343+
}
344+
}
345+
```
321346

322347
Rust:
323348

@@ -578,6 +603,32 @@ func levelOrderBottom(_ root: TreeNode?) -> [[Int]] {
578603
}
579604
```
580605

606+
607+
Scala:
608+
```scala
609+
// 107.二叉树的层次遍历II
610+
object Solution {
611+
import scala.collection.mutable
612+
def levelOrderBottom(root: TreeNode): List[List[Int]] = {
613+
val res = mutable.ListBuffer[List[Int]]()
614+
if (root == null) return res.toList
615+
val queue = mutable.Queue[TreeNode]()
616+
queue.enqueue(root)
617+
while (!queue.isEmpty) {
618+
val tmp = mutable.ListBuffer[Int]()
619+
val len = queue.size
620+
for (i <- 0 until len) {
621+
val curNode = queue.dequeue()
622+
tmp.append(curNode.value)
623+
if (curNode.left != null) queue.enqueue(curNode.left)
624+
if (curNode.right != null) queue.enqueue(curNode.right)
625+
}
626+
res.append(tmp.toList)
627+
}
628+
// 最后翻转一下
629+
res.reverse.toList
630+
}
631+
581632
Rust:
582633

583634
```rust

‎problems/二叉树的统一迭代法.md‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,80 @@ function postorderTraversal(root: TreeNode | null): number[] {
591591
return res;
592592
};
593593
```
594+
Scala:
595+
```scala
596+
// 前序遍历
597+
object Solution {
598+
import scala.collection.mutable
599+
def preorderTraversal(root: TreeNode): List[Int] = {
600+
val res = mutable.ListBuffer[Int]()
601+
val stack = mutable.Stack[TreeNode]()
602+
if (root != null) stack.push(root)
603+
while (!stack.isEmpty) {
604+
var curNode = stack.top
605+
if (curNode != null) {
606+
stack.pop()
607+
if (curNode.right != null) stack.push(curNode.right)
608+
if (curNode.left != null) stack.push(curNode.left)
609+
stack.push(curNode)
610+
stack.push(null)
611+
} else {
612+
stack.pop()
613+
res.append(stack.pop().value)
614+
}
615+
}
616+
res.toList
617+
}
618+
}
619+
620+
// 中序遍历
621+
object Solution {
622+
import scala.collection.mutable
623+
def inorderTraversal(root: TreeNode): List[Int] = {
624+
val res = mutable.ListBuffer[Int]()
625+
val stack = mutable.Stack[TreeNode]()
626+
if (root != null) stack.push(root)
627+
while (!stack.isEmpty) {
628+
var curNode = stack.top
629+
if (curNode != null) {
630+
stack.pop()
631+
if (curNode.right != null) stack.push(curNode.right)
632+
stack.push(curNode)
633+
stack.push(null)
634+
if (curNode.left != null) stack.push(curNode.left)
635+
} else {
636+
// 等于空的时候好办,弹出这个元素
637+
stack.pop()
638+
res.append(stack.pop().value)
639+
}
640+
}
641+
res.toList
642+
}
643+
}
594644

645+
// 后序遍历
646+
object Solution {
647+
import scala.collection.mutable
648+
def postorderTraversal(root: TreeNode): List[Int] = {
649+
val res = mutable.ListBuffer[Int]()
650+
val stack = mutable.Stack[TreeNode]()
651+
if (root != null) stack.push(root)
652+
while (!stack.isEmpty) {
653+
var curNode = stack.top
654+
if (curNode != null) {
655+
stack.pop()
656+
stack.push(curNode)
657+
stack.push(null)
658+
if (curNode.right != null) stack.push(curNode.right)
659+
if (curNode.left != null) stack.push(curNode.left)
660+
} else {
661+
stack.pop()
662+
res.append(stack.pop().value)
663+
}
664+
}
665+
res.toList
666+
}
667+
}
668+
```
595669
-----------------------
596670
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /