Skip to content

Commit 9feff0a

Browse files
authored
Merge pull request #1525 from Samuel-Hinchliffe/main
Create: 203. Remove Linked List Elements, 94-Binary-Tree-Inorder-Traversal, 112-Path-Sum, Trim-a-Binary-Search-Tree., 152-Maximum-Product-Subarray,
2 parents 71394d3 + 4920929 commit 9feff0a

7 files changed

+193
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @param {number} val
11+
* @return {ListNode}
12+
*/
13+
var removeElements = function (head, val) {
14+
15+
let sentinel_node = new ListNode(0, head);
16+
let slow_pointer = sentinel_node;
17+
let fast_pointer = null;
18+
19+
while (slow_pointer) {
20+
// get next legible node
21+
fast_pointer = slow_pointer.next;
22+
while (fast_pointer && fast_pointer.val === val) {
23+
fast_pointer = fast_pointer.next;
24+
}
25+
26+
// Set next node to the legible node
27+
slow_pointer.next = fast_pointer;
28+
slow_pointer = slow_pointer.next;
29+
}
30+
31+
return sentinel_node.next;
32+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @param {number} low
12+
* @param {number} high
13+
* @return {TreeNode}
14+
*/
15+
var trimBST = function (root, low, high) {
16+
17+
if (!root) {
18+
return null;
19+
}
20+
21+
if (root.val < low) {
22+
return trimBST(root.right, low, high);
23+
}
24+
25+
if (root.val > high) {
26+
return trimBST(root.left, low, high);
27+
}
28+
29+
root.left = trimBST(root.left, low, high);
30+
root.right = trimBST(root.right, low, high);
31+
32+
return root;
33+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number[]}
12+
*/
13+
var inorderTraversal = function (root, list = []) {
14+
15+
if (!root) return [];
16+
17+
inorderTraversal(root.left, list);
18+
list.push(root.val)
19+
inorderTraversal(root.right, list);
20+
21+
return list
22+
};

ruby/152-Maximum-Product-Subarray.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# @param {Integer[]} nums
2+
# @return {Integer}
3+
def max_product(nums)
4+
5+
@largest = -Float::INFINITY
6+
@min = 1
7+
@max = 1
8+
9+
for num in nums do
10+
@temp = @max * num
11+
@max = [num * @max, num * @min, num].max
12+
@min = [num * @min, @temp, num].min
13+
@largest = [@max, @largest, num].max
14+
end
15+
16+
return @largest
17+
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function removeElements(head: ListNode | null, val: number): ListNode | null {
14+
15+
let sentinel_node : ListNode = new ListNode(0, head);
16+
let slow_pointer : ListNode | null = sentinel_node;
17+
let fast_pointer : ListNode | null = null;
18+
19+
while (slow_pointer) {
20+
21+
// get next legible node
22+
fast_pointer = slow_pointer.next;
23+
while (fast_pointer && fast_pointer.val === val) {
24+
fast_pointer = fast_pointer.next;
25+
}
26+
27+
// Set next node to the legible node
28+
slow_pointer.next = fast_pointer;
29+
slow_pointer = slow_pointer.next;
30+
}
31+
32+
return sentinel_node.next;
33+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 trimBST(root: TreeNode | null, low: number, high: number): TreeNode | null {
16+
17+
if (!root) {
18+
return null;
19+
}
20+
21+
if (root.val < low) {
22+
return trimBST(root.right, low, high);
23+
}
24+
25+
if (root.val > high) {
26+
return trimBST(root.left, low, high);
27+
}
28+
29+
root.left = trimBST(root.left, low, high);
30+
root.right = trimBST(root.right, low, high);
31+
32+
return root;
33+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
function inorderTraversal(root: TreeNode | null, list: Array<number> = [] ): number[] {
15+
16+
if (!root) return [];
17+
18+
inorderTraversal(root.left, list);
19+
list.push(root.val)
20+
inorderTraversal(root.right, list);
21+
22+
return list
23+
};

0 commit comments

Comments
 (0)