Skip to content

Commit 1985192

Browse files
author
Li Li
committed
add code of 235
1 parent cd9aa95 commit 1985192

File tree

3 files changed

+10
-22
lines changed

3 files changed

+10
-22
lines changed

LeetcodeCShaprDotNetCore/Program.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,6 @@ namespace LeetcodeCShaprDotNetCore {
44
class Program {
55
static void Main(string[] args) {
66

7-
87
}
98
}
109
}
11-
12-
13-
/*
14-
Given "abcabcbb", the answer is "abc", which the length is 3.
15-
16-
Given "bbbbb", the answer is "b", with the length of 1.
17-
18-
Given "pwwkew", the answer is "wke", with the length of 3.
19-
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
20-
*/

LeetcodeCShaprDotNetCore/Solution.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55

66
namespace LeetcodeCShaprDotNetCore {
77
public class Solution {
8-
public TreeNode SortedArrayToBST(int[] nums) {
9-
return SortedArrayToBST(nums, 0, nums.Length - 1);
10-
}
11-
private TreeNode SortedArrayToBST(int[] nums, int left, int right) {
12-
if (nums == null || nums.Length == 0 || left > right) return null;
13-
int mid = left + (right - left) / 2;
14-
TreeNode root = new TreeNode(nums[mid]);
15-
root.left = SortedArrayToBST(nums, left, mid - 1);
16-
root.right = SortedArrayToBST(nums, mid + 1, right);
17-
return root;
8+
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
9+
if (root.val > p.val && root.val > q.val) {
10+
return LowestCommonAncestor(root.left, p, q);
11+
} else if (root.val < p.val && root.val < q.val) {
12+
return LowestCommonAncestor(root.right, p, q);
13+
} else {
14+
return root;
15+
}
1816
}
1917
}
2018
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
I will use this repository to make notes for leetcode solutions and classify the problems by different topics.
44

55
## Solved Problem List
6-
Total solved: 89
6+
Total solved: 90
77

88
### microsoft-labed
99
ID | Difficulty | Tags | Solution | Similar Problem
@@ -12,6 +12,7 @@ ID | Difficulty | Tags | Solution | Similar Problem
1212
419 | Medium | Two dimentional array | [Battleships in a Board](https://github.com/kflili/LeetcodeCShaprDotNetCore/blob/master/top-interview-questions/419.%20Battleships%20in%20a%20Board.cs)
1313
273 | Hard | Math, string | [Integer to English Words](https://github.com/kflili/LeetcodeCShaprDotNetCore/blob/master/top-interview-questions/273.%20Integer%20to%20English%20Words.cs)
1414
54 | Medium | Array | [Spiral Matrix](https://github.com/kflili/LeetcodeCShaprDotNetCore/blob/master/top-interview-questions/54.%20Spiral%20Matrix.cs)
15+
235 | Medium | BST | [Lowest Common Ancestor of a Binary Search Tree](https://github.com/kflili/LeetcodeCShaprDotNetCore/blob/master/top-interview-questions/235.%20Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree.cs)
1516

1617

1718
### top-interview-questions

0 commit comments

Comments
 (0)