mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加 0236.二叉树的最近公共祖先.md Scala版本
This commit is contained in:
@ -343,7 +343,25 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Scala
|
||||||
|
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
def lowestCommonAncestor(root: TreeNode, p: TreeNode, q: TreeNode): TreeNode = {
|
||||||
|
// 递归结束条件
|
||||||
|
if (root == null || root == p || root == q) {
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
|
||||||
|
var left = lowestCommonAncestor(root.left, p, q)
|
||||||
|
var right = lowestCommonAncestor(root.right, p, q)
|
||||||
|
|
||||||
|
if (left != null && right != null) return root
|
||||||
|
if (left == null) return right
|
||||||
|
left
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<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