LeetCode Weekly Contest 24

LeetCode Weekly Contest 24
[2017-03-19]

  1. Diameter of Binary Tree
  2. Convert BST to Greater Tree
  3. 01 Matrix
  4. Output Contest Matches

Diameter of Binary Tree

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

Example:
Given a binary tree

1
2
3
4
5
1
/ \
2 3
/ \
4 5

Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].

Note: The length of path between two nodes is represented by the number of edges between them.

##code
For every node, length of longest path which pass it = MaxDepth of its left subtree + MaxDepth of its right subtree.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Solution {
int max = 0;
public int diameterOfBinaryTree(TreeNode root) {
maxDepth(root);
return max;
}
private int maxDepth(TreeNode root) {
if (root == null) return 0;
int left = maxDepth(root.left);
int right = maxDepth(root.right);
max = Math.max(max, left + right);
return Math.max(left, right) + 1;
}
}


Convert BST to Greater Tree

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.

Example:

1
2
3
4
5
6
7
8
9
Input: The root of a Binary Search Tree like this:
5
/ \
2 13
Output: The root of a Greater Tree like this:
18
/ \
20 13

code

BST中序遍历为升序的,因为先左子树然后中间节点,然后右子树,
这里先右子树然后中间然后左子树,累加右边的即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Solution {
int sum = 0;
public TreeNode convertBST(TreeNode root) {
convert(root);
return root;
}
public void convert(TreeNode cur) {
if (cur == null) return;
convert(cur.right);
sum += cur.val;
cur.val += sum - cur.val;
convert(cur.left);
}
}


01 Matrix

Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.

The distance between two adjacent cells is 1.
Example 1:
Input:

1
2
3
0 0 0
0 1 0
0 0 0

Output:

1
2
3
0 0 0
0 1 0
0 0 0

Example 2:
Input:

1
2
3
0 0 0
0 1 0
1 1 1

Output:

1
2
3
0 0 0
0 1 0
1 2 1

Note:

  1. The number of elements of the given matrix will not exceed 10,000.
  2. There are at least one 0 in the given matrix.
  3. The cells are adjacent in only four directions: up, down, left and right.

code

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
public IList<IList<int>> UpdateMatrix(IList<IList<int>> matrix) {
Queue<int> queue = new Queue<int>();
for (int i = 0; i < matrix.Count; i++)
{
for (int j = 0; j < matrix[i].Count; j++)
{
if (matrix[i][j] == 0)
{
queue.Enqueue(i);
queue.Enqueue(j);
}
else
{
matrix[i][j] = int.MaxValue;
}
}
}
while (queue.Count > 0)
{
int i = queue.Dequeue();
int j = queue.Dequeue();
if (i > 0 && matrix[i][j] + 1 < matrix[i-1][j])
{
matrix[i-1][j] = matrix[i][j] + 1;
queue.Enqueue(i-1);
queue.Enqueue(j);
}
if (i < matrix.Count - 1 && matrix[i][j] + 1 < matrix[i+1][j])
{
matrix[i+1][j] = matrix[i][j] + 1;
queue.Enqueue(i+1);
queue.Enqueue(j);
}
if (j > 0 && matrix[i][j] + 1 < matrix[i][j-1])
{
matrix[i][j-1] = matrix[i][j] + 1;
queue.Enqueue(i);
queue.Enqueue(j-1);
}
if (j < matrix[i].Count - 1 && matrix[i][j] + 1 < matrix[i][j+1])
{
matrix[i][j+1] = matrix[i][j] + 1;
queue.Enqueue(i);
queue.Enqueue(j+1);
}
}
return matrix;
}

Output Contest Matches

During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, like make the rank 1 team play with the rank nth team, which is a good strategy to make the contest more interesting. Now, you’re given n teams, you need to output their final contest matches in the form of a string.

The n teams are given in the form of positive integers from 1 to n, which represents their initial rank. (Rank 1 is the strongest team and Rank n is the weakest team.) We’ll use parentheses(‘(‘, ‘)’) and commas(‘,’) to represent the contest team pairing - parentheses(‘(‘ , ‘)’) for pairing and commas(‘,’) for partition. During the pairing process in each round, you always need to follow the strategy of making the rather strong one pair with the rather weak one.

Example 1:

1
2
3
4
5
Input: 2
Output: (1,2)
Explanation:
Initially, we have the team 1 and the team 2, placed like: 1,2.
Then we pair the team (1,2) together with '(', ')' and ',', which is the final answer.

Example 2:

1
2
3
4
5
6
7
Input: 4
Output: ((1,4),(2,3))
Explanation:
In the first round, we pair the team 1 and 4, the team 2 and 3 together, as we need to make the strong team and weak team together.
And we got (1,4),(2,3).
In the second round, the winners of (1,4) and (2,3) need to play again to generate the final winner, so you need to add the paratheses outside them.
And we got the final answer ((1,4),(2,3)).

Example 3:

1
2
3
4
5
6
7
Input: 8
Output: (((1,8),(4,5)),((2,7),(3,6)))
Explanation:
First round: (1,8),(2,7),(3,6),(4,5)
Second round: ((1,8),(4,5)),((2,7),(3,6))
Third round: (((1,8),(4,5)),((2,7),(3,6)))
Since the third round will generate the final winner, you need to output the answer (((1,8),(4,5)),((2,7),(3,6))).

Note:

  1. The n is in range [2, $2^{12}$].
  2. We ensure that the input n can be converted into the form $2^k$, where k is a positive integer.

code

要实现最强的队伍跟最弱的队伍匹配,就是要讲1~n顺序排列,然后第一队跟倒数第一队匹配,构成一个”(1,n)”,将这个字符串放入另一个链表里,然后将第二队跟倒数第二队匹配,构成”(2,n-1)”,并加入链表里。第一个链表处理完后,递归处理新生成的链表,直到新的链表里字符串的数量为1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Solution {
public String findContestMatch(int n) {
List<String> matches = new ArrayList<>();
for(int i = 1; i <= n; i++) matches.add(String.valueOf(i));
while(matches.size() != 1){
List<String> newRound = new ArrayList<>();
for(int i = 0; i < matches.size()/2; i++)
newRound.add("(" + matches.get(i) + "," + matches.get(matches.size() - i - 1) + ")");
matches = newRound;
}
return matches.get(0);
}
}