Skip to content

Commit 5b30a4e

Browse files
committed
✨ [235] !!
1 parent 4fe6804 commit 5b30a4e

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

235/my_solution.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
9+
/**
10+
* @param {TreeNode} root
11+
* @param {TreeNode} p
12+
* @param {TreeNode} q
13+
* @return {TreeNode}
14+
*/
15+
var lowestCommonAncestor = function (root, p, q) {
16+
let pv = p.val, qv = q.val;
17+
if (pv > qv) {
18+
let tmp = pv;
19+
pv = qv;
20+
qv = tmp;
21+
}
22+
23+
console.log(root.val)
24+
console.log(`p:${pv},rv:${root.val},q:${qv}`)
25+
if (
26+
(pv <= root.val && root.val <= qv)
27+
// || (pv <= root.val && root.val < qv)
28+
// || (pv > root.val && root.val >= qv)
29+
) {
30+
31+
console.log("A")
32+
console.log(root);
33+
console.log(root.val);
34+
return root;
35+
} else if (pv < root.val && qv < root.val) {
36+
console.log("B")
37+
return lowestCommonAncestor(root.left, p, q);
38+
} else if (pv > root.val && qv > root.val) {
39+
console.log("C")
40+
return lowestCommonAncestor(root.right, p, q);
41+
}
42+
console.log("nth???")
43+
};

235/solution.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
9+
/**
10+
* @param {TreeNode} root
11+
* @param {TreeNode} p
12+
* @param {TreeNode} q
13+
* @return {TreeNode}
14+
*/
15+
var lowestCommonAncestor = function (root, p, q) {
16+
let pv = p.val, qv = q.val;
17+
if (pv > qv) {
18+
let tmp = pv;
19+
pv = qv;
20+
qv = tmp;
21+
}
22+
23+
if (pv <= root.val && root.val <= qv) {
24+
return root;
25+
} else if (pv < root.val && qv < root.val) {
26+
return lowestCommonAncestor(root.left, p, q);
27+
} else if (pv > root.val && qv > root.val) {
28+
return lowestCommonAncestor(root.right, p, q);
29+
}
30+
};

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
- [213. House Robber II](./213/)
6060
- [217. Contains Duplicate](./217/)
6161
- [226. Invert Binary Tree](./226/)
62+
- [235. Lowest Common Ancestor of a Binary Search Tree](./235/)
6263
- [238. Product of Array Except Self](./238/)
6364
- [242. Valid Anagram](./242/)
6465
- [268. Missing Number](./268/)
@@ -136,15 +137,15 @@ node ./.../solution.js
136137
```
137138

138139
<!--
139-
TODO: Rmb to dd to TOC!
140+
TODO: Rmb to it into the TOC!
140141
-->
141142

142143
Batch create:
143144
<!--
144145
NOTE: JS IS HERE
145146
-->
146147
```ssh
147-
chapter=98 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
148+
chapter=235 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
148149
```
149150
> then you can use `x` for quick debug.
150151

0 commit comments

Comments
 (0)