LeetCode Notes 018

Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Example:

1
2
3
4
5
6
7
8
9
Given the sorted array: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

0
/ \
-3 9
/ /
-10 5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {number[]} nums
* @return {TreeNode}
*/
var sortedArrayToBST = function(nums) {
return dfs(0, nums.length - 1);

function dfs(start, end) {
if (start > end)
return null;

var mid = (start + end) >> 1;
var node = new TreeNode(nums[mid]);
node.left = dfs(start, mid - 1);
node.right = dfs(mid + 1, end);

return node;
}
};

Convert Sorted List to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Example:

1
2
3
4
5
6
7
8
9
Given the sorted linked list: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

0
/ \
-3 9
/ /
-10 5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {number[]} nums
* @return {TreeNode}
*/
var sortedArrayToBST = function(nums) {
return dfs(0, nums.length - 1);

function dfs(start, end) {
if (start > end)
return null;

var mid = (start + end) >> 1;
var node = new TreeNode(nums[mid]);
node.left = dfs(start, mid - 1);
node.right = dfs(mid + 1, end);

return node;
}
};


/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {ListNode} head
* @return {TreeNode}
*/
var sortedListToBST = function(head) {
var ans = [];
while (head) {
ans.push(head.val);
head = head.next;
}

return sortedArrayToBST(ans);
};

Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Definition for singly-linked list with a random pointer.
* function RandomListNode(label) {
* this.label = label;
* this.next = this.random = null;
* }
*/

/**
* @param {RandomListNode} head
* @return {RandomListNode}
*/
var copyRandomList = function(head) {
if (!head)
return null;

let hash = new Map();
let newArr = [];
let node = head;

while (node) {
hash.set(node, newArr.length);
newArr.push(new RandomListNode(node.label));
node = node.next;
}

node = head;
for (let i = 0, len = newArr.length; i < len; i++) {
if (i !== len - 1)
newArr[i].next = newArr[i + 1];

let random = node.random;
let index = hash.get(random);
if (index !== undefined) {
newArr[i].random = newArr[index];
}

node = node.next;
}

return newArr[0];
};
文章作者: Monad Kai
文章链接: onlookerliu.github.io/2018/03/23/LeetCode-Notes-018/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Code@浮生记
支付宝打赏
微信打赏