Skip to content

Commit a5e177d

Browse files
committed
feat: add typescript solution to lc problem: No.0235.Lowest Common Ancestor of a Binary Search Tree
1 parent 34e3e80 commit a5e177d

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/README.md

+56
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,62 @@ class Solution {
145145
}
146146
```
147147

148+
### **TypeScript**
149+
150+
迭代:
151+
152+
```ts
153+
/**
154+
* Definition for a binary tree node.
155+
* class TreeNode {
156+
* val: number
157+
* left: TreeNode | null
158+
* right: TreeNode | null
159+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
160+
* this.val = (val===undefined ? 0 : val)
161+
* this.left = (left===undefined ? null : left)
162+
* this.right = (right===undefined ? null : right)
163+
* }
164+
* }
165+
*/
166+
167+
function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null {
168+
while (root) {
169+
if (root.val > p.val && root.val > q.val) {
170+
root = root.left;
171+
} else if (root.val < p.val && root.val < q.val) {
172+
root = root.right;
173+
} else {
174+
return root;
175+
}
176+
}
177+
};
178+
```
179+
180+
递归:
181+
182+
```ts
183+
/**
184+
* Definition for a binary tree node.
185+
* class TreeNode {
186+
* val: number
187+
* left: TreeNode | null
188+
* right: TreeNode | null
189+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
190+
* this.val = (val===undefined ? 0 : val)
191+
* this.left = (left===undefined ? null : left)
192+
* this.right = (right===undefined ? null : right)
193+
* }
194+
* }
195+
*/
196+
197+
function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null {
198+
if (root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
199+
if (root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
200+
return root;
201+
};
202+
```
203+
148204
### **Go**
149205

150206
迭代:

solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/README_EN.md

+56
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,62 @@ class Solution {
142142
}
143143
```
144144

145+
### **TypeScript**
146+
147+
迭代:
148+
149+
```ts
150+
/**
151+
* Definition for a binary tree node.
152+
* class TreeNode {
153+
* val: number
154+
* left: TreeNode | null
155+
* right: TreeNode | null
156+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
157+
* this.val = (val===undefined ? 0 : val)
158+
* this.left = (left===undefined ? null : left)
159+
* this.right = (right===undefined ? null : right)
160+
* }
161+
* }
162+
*/
163+
164+
function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null {
165+
while (root) {
166+
if (root.val > p.val && root.val > q.val) {
167+
root = root.left;
168+
} else if (root.val < p.val && root.val < q.val) {
169+
root = root.right;
170+
} else {
171+
return root;
172+
}
173+
}
174+
};
175+
```
176+
177+
递归:
178+
179+
```ts
180+
/**
181+
* Definition for a binary tree node.
182+
* class TreeNode {
183+
* val: number
184+
* left: TreeNode | null
185+
* right: TreeNode | null
186+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
187+
* this.val = (val===undefined ? 0 : val)
188+
* this.left = (left===undefined ? null : left)
189+
* this.right = (right===undefined ? null : right)
190+
* }
191+
* }
192+
*/
193+
194+
function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null {
195+
if (root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
196+
if (root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
197+
return root;
198+
};
199+
```
200+
145201
### **Go**
146202

147203
Iterative:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null {
16+
while (root) {
17+
if (root.val > p.val && root.val > q.val) {
18+
root = root.left;
19+
} else if (root.val < p.val && root.val < q.val) {
20+
root = root.right;
21+
} else {
22+
return root;
23+
}
24+
}
25+
};

0 commit comments

Comments
 (0)