File tree 3 files changed +76
-2
lines changed
3 files changed +76
-2
lines changed Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change 59
59
- [ 213. House Robber II] ( ./213/ )
60
60
- [ 217. Contains Duplicate] ( ./217/ )
61
61
- [ 226. Invert Binary Tree] ( ./226/ )
62
+ - [ 235. Lowest Common Ancestor of a Binary Search Tree] ( ./235/ )
62
63
- [ 238. Product of Array Except Self] ( ./238/ )
63
64
- [ 242. Valid Anagram] ( ./242/ )
64
65
- [ 268. Missing Number] ( ./268/ )
@@ -136,15 +137,15 @@ node ./.../solution.js
136
137
```
137
138
138
139
<!--
139
- TODO: Rmb to dd to TOC!
140
+ TODO: Rmb to it into the TOC!
140
141
-->
141
142
142
143
Batch create:
143
144
<!--
144
145
NOTE: JS IS HERE
145
146
-->
146
147
``` 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"
148
149
```
149
150
> then you can use ` x ` for quick debug.
150
151
You can’t perform that action at this time.
0 commit comments