mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加0235.二叉搜索树的最近公共祖先.md递归Java解法
This commit is contained in:
@ -234,7 +234,15 @@ public:
|
||||
## Java
|
||||
|
||||
递归法:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
||||
if (root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
|
||||
if (root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
|
||||
return root;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
迭代法:
|
||||
```java
|
||||
|
Reference in New Issue
Block a user