Skip to content

Commit 606a2bc

Browse files
trees and graphs
1 parent 84fb004 commit 606a2bc

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Given a binary tree
3+
4+
struct Node {
5+
int val;
6+
Node *left;
7+
Node *right;
8+
Node *next;
9+
}
10+
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
11+
12+
Initially, all next pointers are set to NULL.
13+
14+
15+
16+
Follow up:
17+
18+
You may only use constant extra space.
19+
Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.
20+
*/
21+
/**
22+
* // Definition for a Node.
23+
* function Node(val, left, right, next) {
24+
* this.val = val === undefined ? null : val;
25+
* this.left = left === undefined ? null : left;
26+
* this.right = right === undefined ? null : right;
27+
* this.next = next === undefined ? null : next;
28+
* };
29+
*/
30+
let prev, leftMost;
31+
32+
const processChild = childNode => {
33+
if (childNode !== null) {
34+
if (prev !== null) {
35+
// If a node exists at this level already, assign the childNode as next of prev
36+
prev.next = childNode;
37+
} else {
38+
// else this is the first node on this level
39+
leftMost = childNode;
40+
}
41+
prev = childNode;
42+
}
43+
}
44+
const connect = root => {
45+
if (root === null) {
46+
return root;
47+
}
48+
leftMost = root;
49+
50+
while (leftMost !== null) {
51+
// No nodes before this at this level
52+
prev = null;
53+
// Node to keep track of the current level in Tree
54+
let curr = leftMost;
55+
// Now that we have saved the current node, reassign the leftMost to null
56+
leftMost = null;
57+
58+
while (curr !== null) {
59+
processChild(curr.left);
60+
processChild(curr.right);
61+
curr = curr.next;
62+
}
63+
}
64+
return root;
65+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
3+
4+
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
5+
6+
Example 1:
7+
8+
Given nums = [1,1,2],
9+
10+
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
11+
12+
It doesn't matter what you leave beyond the returned length.
13+
Example 2:
14+
15+
Given nums = [0,0,1,1,1,2,2,3,3,4],
16+
17+
Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
18+
19+
It doesn't matter what values are set beyond the returned length.
20+
*/
21+
22+
const removeDuplicates = nums => {
23+
if (nums.length === 0) {
24+
return 0;
25+
}
26+
27+
let i = 0;
28+
for (let j = 1; j < nums.length; j++) {
29+
if (nums[i] !== nums[j]) {
30+
i++;
31+
nums[i] = nums[j];
32+
}
33+
}
34+
35+
return i + 1;
36+
}

0 commit comments

Comments
 (0)