We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0e3a1bc commit 9412e2eCopy full SHA for 9412e2e
problems/0700.二叉搜索树中的搜索.md
@@ -363,7 +363,34 @@ function searchBST(root: TreeNode | null, val: number): TreeNode | null {
363
};
364
```
365
366
+## Scala
367
+
368
+递归:
369
+```scala
370
+object Solution {
371
+ def searchBST(root: TreeNode, value: Int): TreeNode = {
372
+ if (root == null || value == root.value) return root
373
+ // 相当于三元表达式,在Scala中if...else有返回值
374
+ if (value < root.value) searchBST(root.left, value) else searchBST(root.right, value)
375
+ }
376
+}
377
+```
378
379
+迭代:
380
381
382
383
+ // 因为root是不可变量,所以需要赋值给一个可变量
384
+ var node = root
385
+ while (node != null) {
386
+ if (value < node.value) node = node.left
387
+ else if (value > node.value) node = node.right
388
+ else return node
389
390
+ null // 没有返回就返回空
391
392
393
394
395
-----------------------
396
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments