Skip to content

Commit e36760f

Browse files
authored
Create: 235-Lowest-Common-Ancestor-of-a-Binary-Search-Tree.cs
1 parent e760c20 commit e36760f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public int val;
5+
* public TreeNode left;
6+
* public TreeNode right;
7+
* public TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
11+
public class Solution {
12+
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
13+
// Traverse Right child
14+
if (p.val > root.val && q.val > root.val) {
15+
return LowestCommonAncestor(root.right, p, q);
16+
}
17+
18+
// Traverse Left Child
19+
if (p.val < root.val && q.val < root.val) {
20+
return LowestCommonAncestor(root.left, p, q);
21+
}
22+
23+
return root;
24+
}
25+
}

0 commit comments

Comments
 (0)