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 95cc2b9

Browse files
committed
添加 0236.二叉树的最近公共祖先.md Scala版本
1 parent f6456c3 commit 95cc2b9

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

‎problems/0236.二叉树的最近公共祖先.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,25 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree
343343
};
344344
```
345345

346+
## Scala
347+
348+
```scala
349+
object Solution {
350+
def lowestCommonAncestor(root: TreeNode, p: TreeNode, q: TreeNode): TreeNode = {
351+
// 递归结束条件
352+
if (root == null || root == p || root == q) {
353+
return root
354+
}
355+
356+
var left = lowestCommonAncestor(root.left, p, q)
357+
var right = lowestCommonAncestor(root.right, p, q)
346358

359+
if (left != null && right != null) return root
360+
if (left == null) return right
361+
left
362+
}
363+
}
364+
```
347365

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

0 commit comments

Comments
(0)

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