Skip to content

Commit c482529

Browse files
authored
Merge pull request neetcode-gh#361 from Chrislee1996/main
Created 235-lowest-common-ancestor-of-a-BST and 66-Plus-One for Javascript
2 parents 96d23fe + 12fdcd4 commit c482529

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-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+
* 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+
while(root !== null) {
17+
if (root.val < p.val && root.val < q.val ) {
18+
root = root.right
19+
} else if (root.val > p.val && root.val > q.val) {
20+
root = root.left
21+
} else {
22+
return root
23+
}
24+
}
25+
};

javascript/66-plus-one.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} digits
3+
* @return {number[]}
4+
*/
5+
var plusOne = function(digits) {
6+
for (let i = digits.length-1 ; i >= 0 ; i--) {
7+
if (digits[i] !== 9) {
8+
digits[i]++
9+
return digits
10+
} else {
11+
digits[i] = 0
12+
}
13+
}
14+
digits.unshift(1)
15+
return digits
16+
};

0 commit comments

Comments
 (0)