Skip to content

Commit fa4ca89

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 235_Lowest_Common_Ancestor_of_a_Binary_Search_Tree.java
1 parent f3682aa commit fa4ca89

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
3+
while (root != null) {
4+
if (root.val > p.val && root.val > q.val) {
5+
root = root.left;
6+
} else if (root.val < p.val && root.val < q.val) {
7+
root = root.right;
8+
} else {
9+
break;
10+
}
11+
}
12+
13+
return root;
14+
}
15+
}

0 commit comments

Comments
 (0)