Skip to content

Commit 8363211

Browse files
authored
Merge pull request #131 from sounmind/main
2 parents dee9e83 + 2c56ca9 commit 8363211

File tree

5 files changed

+204
-0
lines changed
  • binary-tree-level-order-traversal
  • lowest-common-ancestor-of-a-binary-search-tree
  • remove-nth-node-from-end-of-list
  • reorder-list
  • validate-binary-search-tree

5 files changed

+204
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @param {TreeNode} root
3+
* @return {number[][]}
4+
*/
5+
var levelOrder = function (root) {
6+
const result = [];
7+
8+
const dfs = (node, level) => {
9+
if (!node) {
10+
return;
11+
}
12+
13+
if (!result[level]) {
14+
result[level] = [];
15+
}
16+
17+
result[level].push(node.val);
18+
19+
dfs(node.left, level + 1);
20+
dfs(node.right, level + 1);
21+
};
22+
23+
dfs(root, 0);
24+
25+
return result;
26+
};
27+
28+
/**
29+
* Time Complexity: O(n)
30+
* - The function visits each node exactly once, making it O(n).
31+
* - Here, n is the number of nodes in the binary tree.
32+
*
33+
* Space Complexity: O(n)
34+
* - The space complexity is determined by the recursion stack.
35+
* - In the worst case, the recursion stack could store all nodes in the binary tree.
36+
* - Thus, the space complexity is O(n).
37+
*/
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
* @param {TreeNode} root
10+
* @param {TreeNode} p
11+
* @param {TreeNode} q
12+
* @return {TreeNode}
13+
*/
14+
var lowestCommonAncestor = function (root, p, q) {
15+
if (root.val < p.val && root.val < q.val) {
16+
return lowestCommonAncestor(root.right, p, q);
17+
}
18+
19+
if (root.val > p.val && root.val > q.val) {
20+
return lowestCommonAncestor(root.left, p, q);
21+
}
22+
23+
return root;
24+
};
25+
26+
/**
27+
* Time Complexity: O(h), where h is the height of the binary search tree.
28+
* Space Complexity: O(h), where h is the height of the binary search tree.
29+
*/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {ListNode} head
3+
* @param {number} n
4+
* @return {ListNode}
5+
*/
6+
function removeNthFromEnd(head, n) {
7+
let dummy = new ListNode(0);
8+
dummy.next = head;
9+
let first = dummy;
10+
let second = dummy;
11+
12+
// Move the first pointer n+1 steps ahead
13+
for (let i = 0; i <= n; i++) {
14+
first = first.next;
15+
}
16+
17+
// Move both pointers until the first pointer reaches the end
18+
// Second pointer will be at the (n+1)th node from the end
19+
while (first !== null) {
20+
first = first.next;
21+
second = second.next;
22+
}
23+
24+
// Remove the nth node from the end
25+
second.next = second.next.next;
26+
27+
return dummy.next;
28+
}
29+
30+
/**
31+
* Time Complexity: O(n)
32+
* Space Complexity: O(1)
33+
*/

reorder-list/evan.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
* @return {void} Do not return anything, modify head in-place instead.
11+
*/
12+
var reorderList = function (head) {
13+
const middleNode = findMiddleNode(head);
14+
let reversedHalf = reverseList(middleNode.next);
15+
middleNode.next = null;
16+
17+
let half = head;
18+
19+
while (reversedHalf) {
20+
const nextForward = half.next;
21+
const nextBackward = reversedHalf.next;
22+
23+
half.next = reversedHalf;
24+
reversedHalf.next = nextForward;
25+
26+
reversedHalf = nextBackward;
27+
half = nextForward;
28+
}
29+
};
30+
/**
31+
* Time Complexity: O(n)
32+
* - Finding the middle node takes O(n).
33+
* - Reversing the second half takes O(n).
34+
* - Merging the two halves takes O(n).
35+
* - Overall, the time complexity is O(n) + O(n) + O(n) = O(n).
36+
*
37+
* Space Complexity: O(1)
38+
* - We only use a constant amount of extra space (pointers),
39+
* so the space complexity is O(1).
40+
*/
41+
42+
/**
43+
* @param {ListNode} head
44+
* @return {ListNode}
45+
*/
46+
var reverseList = function (head) {
47+
let currentNode = head;
48+
let previousNode = null;
49+
50+
while (currentNode !== null) {
51+
const nextNode = currentNode.next;
52+
53+
currentNode.next = previousNode;
54+
previousNode = currentNode;
55+
currentNode = nextNode;
56+
}
57+
58+
return previousNode;
59+
};
60+
61+
/**
62+
* @param {ListNode} head
63+
* @return {ListNode}
64+
*/
65+
var findMiddleNode = function (head) {
66+
if (!head) return null;
67+
68+
let slow = head;
69+
let fast = head;
70+
71+
while (fast !== null && fast.next !== null) {
72+
slow = slow.next;
73+
fast = fast.next.next;
74+
}
75+
76+
return slow;
77+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 {boolean}
12+
*/
13+
function isValidBST(root, low = -Infinity, high = Infinity) {
14+
if (root === null) {
15+
return true;
16+
}
17+
18+
if (root.val <= low || root.val >= high) {
19+
return false;
20+
}
21+
22+
return (
23+
// left root should be less than current root
24+
isValidBST(root.left, low, root.val) &&
25+
// right root should be greater than current root
26+
isValidBST(root.right, root.val, high)
27+
);
28+
}

0 commit comments

Comments
 (0)