Skip to content

Commit 0e93e0e

Browse files
authored
Create Lowest Common Ancestor of a Binary Search Tree.java
1 parent 6a4678a commit 0e93e0e

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//235. Lowest Common Ancestor of a Binary Search Tree
2+
class Solution {
3+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
4+
if (root.val > Math.max(p.val, q.val))
5+
return lowestCommonAncestor(root.left, p, q);
6+
if (root.val < Math.min(p.val, q.val))
7+
return lowestCommonAncestor(root.right, p, q);
8+
return root;
9+
}
10+
}

0 commit comments

Comments
 (0)