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 23cc663

Browse files
Merge pull request youngyangyang04#1415 from wzqwtt/tree14
添加(0530.二叉搜索树的最小绝对差、0501.二叉搜索树中的众数) Scala版本
2 parents cf8582d + dc8fcd5 commit 23cc663

File tree

2 files changed

+149
-1
lines changed

2 files changed

+149
-1
lines changed

‎problems/0501.二叉搜索树中的众数.md

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
1010
# 501.二叉搜索树中的众数
1111

12-
[力扣题目链接](https://leetcode.cn/problems/find-mode-in-binary-search-tree/solution/)
12+
13+
[力扣题目链接](https://leetcode.cn/problems/find-mode-in-binary-search-tree/)
14+
1315

1416
给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素)。
1517

@@ -798,7 +800,76 @@ function findMode(root: TreeNode | null): number[] {
798800
};
799801
```
800802

803+
## Scala
804+
805+
暴力:
806+
```scala
807+
object Solution {
808+
// 导包
809+
import scala.collection.mutable // 集合包
810+
import scala.util.control.Breaks.{break, breakable} // 流程控制包
811+
def findMode(root: TreeNode): Array[Int] = {
812+
var map = mutable.HashMap[Int, Int]() // 存储节点的值,和该值出现的次数
813+
def searchBST(curNode: TreeNode): Unit = {
814+
if (curNode == null) return
815+
var value = map.getOrElse(curNode.value, 0)
816+
map.put(curNode.value, value + 1)
817+
searchBST(curNode.left)
818+
searchBST(curNode.right)
819+
}
820+
searchBST(root) // 前序遍历把每个节点的值加入到里面
821+
// 将map转换为list,随后根据元组的第二个值进行排序
822+
val list = map.toList.sortWith((map1, map2) => {
823+
if (map1._2 > map2._2) true else false
824+
})
825+
var res = mutable.ArrayBuffer[Int]()
826+
res.append(list(0)._1) // 将第一个加入结果集
827+
breakable {
828+
for (i <- 1 until list.size) {
829+
// 如果值相同就加入结果集合,反之break
830+
if (list(i)._2 == list(0)._2) res.append(list(i)._1)
831+
else break
832+
}
833+
}
834+
res.toArray // 最终返回res的Array格式,return关键字可以省略
835+
}
836+
}
837+
```
801838

839+
递归(利用二叉搜索树的性质):
840+
```scala
841+
object Solution {
842+
import scala.collection.mutable
843+
def findMode(root: TreeNode): Array[Int] = {
844+
var maxCount = 0 // 最大频率
845+
var count = 0 // 统计频率
846+
var pre: TreeNode = null
847+
var result = mutable.ArrayBuffer[Int]()
848+
849+
def searchBST(cur: TreeNode): Unit = {
850+
if (cur == null) return
851+
searchBST(cur.left)
852+
if (pre == null) count = 1 // 等于空置为1
853+
else if (pre.value == cur.value) count += 1 // 与上一个节点的值相同加1
854+
else count = 1 // 与上一个节点的值不同
855+
pre = cur
856+
857+
// 如果和最大值相同,则放入结果集
858+
if (count == maxCount) result.append(cur.value)
859+
860+
// 如果当前计数大于最大值频率,更新最大值,清空结果集
861+
if (count > maxCount) {
862+
maxCount = count
863+
result.clear()
864+
result.append(cur.value)
865+
}
866+
searchBST(cur.right)
867+
}
868+
searchBST(root)
869+
result.toArray // return关键字可以省略
870+
}
871+
}
872+
```
802873

803874

804875
-----------------------

‎problems/0530.二叉搜索树的最小绝对差.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,84 @@ function getMinimumDifference(root: TreeNode | null): number {
431431
};
432432
```
433433

434+
## Scala
435+
436+
构建二叉树的有序数组:
437+
438+
```scala
439+
object Solution {
440+
import scala.collection.mutable
441+
def getMinimumDifference(root: TreeNode): Int = {
442+
val arr = mutable.ArrayBuffer[Int]()
443+
def traversal(node: TreeNode): Unit = {
444+
if (node == null) return
445+
traversal(node.left)
446+
arr.append(node.value)
447+
traversal(node.right)
448+
}
449+
traversal(root)
450+
// 在有序数组上求最小差值
451+
var result = Int.MaxValue
452+
for (i <- 1 until arr.size) {
453+
result = math.min(result, arr(i) - arr(i - 1))
454+
}
455+
result // 返回最小差值
456+
}
457+
}
458+
```
434459

460+
递归记录前一个节点:
461+
462+
```scala
463+
object Solution {
464+
def getMinimumDifference(root: TreeNode): Int = {
465+
var result = Int.MaxValue // 初始化为最大值
466+
var pre: TreeNode = null // 记录前一个节点
467+
468+
def traversal(cur: TreeNode): Unit = {
469+
if (cur == null) return
470+
traversal(cur.left)
471+
if (pre != null) {
472+
// 对比result与节点之间的差值
473+
result = math.min(result, cur.value - pre.value)
474+
}
475+
pre = cur
476+
traversal(cur.right)
477+
}
478+
479+
traversal(root)
480+
result // return关键字可以省略
481+
}
482+
}
483+
```
484+
485+
迭代解决:
486+
487+
```scala
488+
object Solution {
489+
import scala.collection.mutable
490+
def getMinimumDifference(root: TreeNode): Int = {
491+
var result = Int.MaxValue // 初始化为最大值
492+
var pre: TreeNode = null // 记录前一个节点
493+
var cur = root
494+
var stack = mutable.Stack[TreeNode]()
495+
while (cur != null || !stack.isEmpty) {
496+
if (cur != null) {
497+
stack.push(cur)
498+
cur = cur.left
499+
} else {
500+
cur = stack.pop()
501+
if (pre != null) {
502+
result = math.min(result, cur.value - pre.value)
503+
}
504+
pre = cur
505+
cur = cur.right
506+
}
507+
}
508+
result // return关键字可以省略
509+
}
510+
}
511+
```
435512

436513
-----------------------
437514
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

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