mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge pull request #1351 from wzqwtt/tree02
添加(二叉树的统一迭代法、102.二叉树的层序遍历、107.二叉树的层次遍历II)Scala版本
This commit is contained in:
@ -318,6 +318,31 @@ func levelOrder(_ root: TreeNode?) -> [[Int]] {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Scala:
|
||||||
|
```scala
|
||||||
|
// 102.二叉树的层序遍历
|
||||||
|
object Solution {
|
||||||
|
import scala.collection.mutable
|
||||||
|
def levelOrder(root: TreeNode): List[List[Int]] = {
|
||||||
|
val res = mutable.ListBuffer[List[Int]]()
|
||||||
|
if (root == null) return res.toList
|
||||||
|
val queue = mutable.Queue[TreeNode]() // 声明一个队列
|
||||||
|
queue.enqueue(root) // 把根节点加入queue
|
||||||
|
while (!queue.isEmpty) {
|
||||||
|
val tmp = mutable.ListBuffer[Int]()
|
||||||
|
val len = queue.size // 求出len的长度
|
||||||
|
for (i <- 0 until len) { // 从0到当前队列长度的所有节点都加入到结果集
|
||||||
|
val curNode = queue.dequeue()
|
||||||
|
tmp.append(curNode.value)
|
||||||
|
if (curNode.left != null) queue.enqueue(curNode.left)
|
||||||
|
if (curNode.right != null) queue.enqueue(curNode.right)
|
||||||
|
}
|
||||||
|
res.append(tmp.toList)
|
||||||
|
}
|
||||||
|
res.toList
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Rust:
|
Rust:
|
||||||
|
|
||||||
@ -578,6 +603,32 @@ func levelOrderBottom(_ root: TreeNode?) -> [[Int]] {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
Scala:
|
||||||
|
```scala
|
||||||
|
// 107.二叉树的层次遍历II
|
||||||
|
object Solution {
|
||||||
|
import scala.collection.mutable
|
||||||
|
def levelOrderBottom(root: TreeNode): List[List[Int]] = {
|
||||||
|
val res = mutable.ListBuffer[List[Int]]()
|
||||||
|
if (root == null) return res.toList
|
||||||
|
val queue = mutable.Queue[TreeNode]()
|
||||||
|
queue.enqueue(root)
|
||||||
|
while (!queue.isEmpty) {
|
||||||
|
val tmp = mutable.ListBuffer[Int]()
|
||||||
|
val len = queue.size
|
||||||
|
for (i <- 0 until len) {
|
||||||
|
val curNode = queue.dequeue()
|
||||||
|
tmp.append(curNode.value)
|
||||||
|
if (curNode.left != null) queue.enqueue(curNode.left)
|
||||||
|
if (curNode.right != null) queue.enqueue(curNode.right)
|
||||||
|
}
|
||||||
|
res.append(tmp.toList)
|
||||||
|
}
|
||||||
|
// 最后翻转一下
|
||||||
|
res.reverse.toList
|
||||||
|
}
|
||||||
|
|
||||||
Rust:
|
Rust:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
@ -591,6 +591,80 @@ function postorderTraversal(root: TreeNode | null): number[] {
|
|||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
Scala:
|
||||||
|
```scala
|
||||||
|
// 前序遍历
|
||||||
|
object Solution {
|
||||||
|
import scala.collection.mutable
|
||||||
|
def preorderTraversal(root: TreeNode): List[Int] = {
|
||||||
|
val res = mutable.ListBuffer[Int]()
|
||||||
|
val stack = mutable.Stack[TreeNode]()
|
||||||
|
if (root != null) stack.push(root)
|
||||||
|
while (!stack.isEmpty) {
|
||||||
|
var curNode = stack.top
|
||||||
|
if (curNode != null) {
|
||||||
|
stack.pop()
|
||||||
|
if (curNode.right != null) stack.push(curNode.right)
|
||||||
|
if (curNode.left != null) stack.push(curNode.left)
|
||||||
|
stack.push(curNode)
|
||||||
|
stack.push(null)
|
||||||
|
} else {
|
||||||
|
stack.pop()
|
||||||
|
res.append(stack.pop().value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res.toList
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 中序遍历
|
||||||
|
object Solution {
|
||||||
|
import scala.collection.mutable
|
||||||
|
def inorderTraversal(root: TreeNode): List[Int] = {
|
||||||
|
val res = mutable.ListBuffer[Int]()
|
||||||
|
val stack = mutable.Stack[TreeNode]()
|
||||||
|
if (root != null) stack.push(root)
|
||||||
|
while (!stack.isEmpty) {
|
||||||
|
var curNode = stack.top
|
||||||
|
if (curNode != null) {
|
||||||
|
stack.pop()
|
||||||
|
if (curNode.right != null) stack.push(curNode.right)
|
||||||
|
stack.push(curNode)
|
||||||
|
stack.push(null)
|
||||||
|
if (curNode.left != null) stack.push(curNode.left)
|
||||||
|
} else {
|
||||||
|
// 等于空的时候好办,弹出这个元素
|
||||||
|
stack.pop()
|
||||||
|
res.append(stack.pop().value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res.toList
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 后序遍历
|
||||||
|
object Solution {
|
||||||
|
import scala.collection.mutable
|
||||||
|
def postorderTraversal(root: TreeNode): List[Int] = {
|
||||||
|
val res = mutable.ListBuffer[Int]()
|
||||||
|
val stack = mutable.Stack[TreeNode]()
|
||||||
|
if (root != null) stack.push(root)
|
||||||
|
while (!stack.isEmpty) {
|
||||||
|
var curNode = stack.top
|
||||||
|
if (curNode != null) {
|
||||||
|
stack.pop()
|
||||||
|
stack.push(curNode)
|
||||||
|
stack.push(null)
|
||||||
|
if (curNode.right != null) stack.push(curNode.right)
|
||||||
|
if (curNode.left != null) stack.push(curNode.left)
|
||||||
|
} else {
|
||||||
|
stack.pop()
|
||||||
|
res.append(stack.pop().value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res.toList
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
Reference in New Issue
Block a user