Skip to content

Commit 19c1298

Browse files
committed
add 70-75
1 parent 4538eeb commit 19c1298

8 files changed

+361
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
3+
*/
4+
/**
5+
* Definition for singly-linked list.
6+
* struct ListNode {
7+
* int val;
8+
* ListNode *next;
9+
* ListNode(int x) : val(x), next(NULL) {}
10+
* };
11+
*/
12+
/**
13+
* Definition for binary tree
14+
* struct TreeNode {
15+
* int val;
16+
* TreeNode *left;
17+
* TreeNode *right;
18+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
19+
* };
20+
*/
21+
class Solution {
22+
public:
23+
TreeNode *sortedListToBST(ListNode *head) {
24+
if(!head)
25+
return NULL;
26+
int len = 0;
27+
ListNode *node = head;
28+
while(node)
29+
{
30+
node = node->next;
31+
++len;
32+
}
33+
return buildHelper(head, 0, len-1);
34+
}
35+
TreeNode *buildHelper(ListNode *&head, int st, int ed)
36+
{
37+
if(st > ed)
38+
return NULL;
39+
int mid = (st+ed)/2;
40+
TreeNode *left = buildHelper(head, st, mid-1);
41+
TreeNode *root = new TreeNode(head->val);
42+
head = head->next;
43+
TreeNode *right = buildHelper(head, mid+1, ed);
44+
root->left = left;
45+
root->right = right;
46+
return root;
47+
}
48+
};

FlattenBinaryTreetoLinkedList.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Given a binary tree, flatten it to a linked list in-place.
3+
4+
For example,
5+
Given
6+
7+
1
8+
/ \
9+
2 5
10+
/ \ \
11+
3 4 6
12+
The flattened tree should look like:
13+
1
14+
\
15+
2
16+
\
17+
3
18+
\
19+
4
20+
\
21+
5
22+
\
23+
6
24+
click to show hints.
25+
26+
Hints:
27+
If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.
28+
*/
29+
/**
30+
* Definition for binary tree
31+
* struct TreeNode {
32+
* int val;
33+
* TreeNode *left;
34+
* TreeNode *right;
35+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
36+
* };
37+
*/
38+
class Solution {
39+
public:
40+
void flatten(TreeNode *root) {
41+
if(!root)
42+
return;
43+
flattenHelper(root);
44+
}
45+
TreeNode *flattenHelper(TreeNode *root)
46+
{
47+
if(!root->left && !root->right)
48+
return root;
49+
TreeNode *rhead = NULL;
50+
if(root->right)
51+
rhead = flattenHelper(root->right);
52+
TreeNode *ltail = root;
53+
if(root->left)
54+
{
55+
TreeNode *lhead = flattenHelper(root->left);
56+
root->left = NULL;
57+
root->right = lhead;
58+
while(ltail->right)
59+
ltail = ltail->right;
60+
}
61+
if(rhead)
62+
{
63+
ltail->right = rhead;
64+
rhead->left = NULL;
65+
}
66+
return root;
67+
}
68+
};

LongestConsecutiveSequence.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
3+
4+
For example,
5+
Given [100, 4, 200, 1, 3, 2],
6+
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
7+
8+
Your algorithm should run in O(n) complexity.
9+
*/
10+
11+
class Solution {
12+
public:
13+
int longestConsecutive(vector<int> &num) {
14+
unordered_set<int> hashset;
15+
for(int i = 0; i < num.size(); ++i)
16+
hashset.insert(num[i]);
17+
int maxlen = 1;
18+
for(int i = 0; i < num.size(); ++i)
19+
{
20+
int inc = num[i];
21+
int des = num[i];
22+
int len = 1;
23+
if(hashset.find(num[i]) == hashset.end())
24+
continue;
25+
while(hashset.find(inc+1) != hashset.end())
26+
{
27+
++len;
28+
hashset.erase(++inc);
29+
}
30+
while(hashset.find(des-1) != hashset.end())
31+
{
32+
++len;
33+
hashset.erase(--des);
34+
}
35+
maxlen = max(maxlen, len);
36+
}
37+
return maxlen;
38+
}
39+
};

PalindromePartitioningII.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Given a string s, partition s such that every substring of the partition is a palindrome.
3+
4+
Return the minimum cuts needed for a palindrome partitioning of s.
5+
6+
For example, given s = "aab",
7+
Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.
8+
*/
9+
10+
class Solution {
11+
public:
12+
int minCut(string s) {
13+
int n = s.size();
14+
bool palin[n][n];
15+
int cut[n+1];
16+
for(int i = 0; i < n; ++i)
17+
for(int j = 0; j < n; ++j)
18+
palin[i][j] = false;
19+
for(int i = 0; i <= n; ++i)
20+
cut[i] = n-i-1;
21+
for(int i = n-1; i >= 0; --i)
22+
{
23+
for(int j = i; j < n; ++j)
24+
{
25+
if(s[i] == s[j] && (j-i < 2 || palin[i+1][j-1]))
26+
{
27+
palin[i][j] = true;
28+
cut[i] = min(cut[i], cut[j+1]+1);
29+
}
30+
}
31+
}
32+
return cut[0];
33+
}
34+
};

PathSum.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
3+
4+
For example:
5+
Given the below binary tree and sum = 22,
6+
5
7+
/ \
8+
4 8
9+
/ / \
10+
11 13 4
11+
/ \ \
12+
7 2 1
13+
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
14+
*/
15+
16+
/**
17+
* Definition for binary tree
18+
* struct TreeNode {
19+
* int val;
20+
* TreeNode *left;
21+
* TreeNode *right;
22+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
23+
* };
24+
*/
25+
class Solution {
26+
public:
27+
bool hasPathSum(TreeNode *root, int sum) {
28+
if(!root)
29+
return false;
30+
return pathHelper(root, sum);
31+
}
32+
bool pathHelper(TreeNode *root, int sum)
33+
{
34+
if(!root)
35+
return false;
36+
else
37+
{
38+
sum -= root->val;
39+
if(sum == 0 && !root->left && !root->right)
40+
return true;
41+
}
42+
return pathHelper(root->left, sum) || pathHelper(root->right, sum);
43+
}
44+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Given a binary tree
3+
4+
struct TreeLinkNode {
5+
TreeLinkNode *left;
6+
TreeLinkNode *right;
7+
TreeLinkNode *next;
8+
}
9+
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.
10+
11+
Initially, all next pointers are set to NULL.
12+
13+
Note:
14+
15+
You may only use constant extra space.
16+
You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
17+
For example,
18+
Given the following perfect binary tree,
19+
1
20+
/ \
21+
2 3
22+
/ \ / \
23+
4 5 6 7
24+
After calling your function, the tree should look like:
25+
1 -> NULL
26+
/ \
27+
2 -> 3 -> NULL
28+
/ \ / \
29+
4->5->6->7 -> NULL
30+
*/
31+
32+
/**
33+
* Definition for binary tree with next pointer.
34+
* struct TreeLinkNode {
35+
* int val;
36+
* TreeLinkNode *left, *right, *next;
37+
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
38+
* };
39+
*/
40+
class Solution {
41+
public:
42+
void connect(TreeLinkNode *root) {
43+
if(!root || (!root->left && !root->right))
44+
return;
45+
root->left->next = root->right;
46+
if(root->next)
47+
root->right->next = root->next->left;
48+
connect(root->left);
49+
connect(root->right);
50+
}
51+
};

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,27 @@ Solve problems in LeetCode http://oj.leetcode.com/.
324324

325325
Recursive call. Middle element is current root.
326326

327+
70. Convert Sorted List to Binary Search Tree
328+
329+
Recursive call. Helper(ListNode *&head, ...) beacuse we need to move head to its next in each recursive call.
330+
After left = helper(head, ...), head now points to middle node, which is the current root.
331+
332+
71. Path Sum
333+
334+
Recursive call. sum -= root->val. If at one leaf, sum == 0. return true.
335+
336+
72. Flatten Binary Tree to Linked List
337+
338+
Flatten right subtree, then left subtree recursively. Then attach right subtree to the end of the left subtree.
339+
340+
73. Longest Consecutive Sequence
341+
342+
Use hashset to store each element. When iterate one element, find the increasing & decreasing order, and erase these numbers.
343+
344+
74. Surrounded Regions
345+
346+
DFS or BFS. Check boundary.
347+
348+
75. Palindrome Partitioning II
349+
350+
DP. Use a 2D array ispalin[i][j] (true, if string(i to j) is parlinfrom). cut[i] = min(cut[i], cut[j+1]+1).

SurroundedRegions.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.
3+
4+
A region is captured by flipping all 'O's into 'X's in that surrounded region .
5+
6+
For example,
7+
X X X X
8+
X O O X
9+
X X O X
10+
X O X X
11+
After running your function, the board should be:
12+
X X X X
13+
X X X X
14+
X X X X
15+
X O X X
16+
*/
17+
18+
class Solution {
19+
public:
20+
void solve(vector<vector<char>> &board) {
21+
if(board.empty())
22+
return;
23+
int m = board.size(), n = board[0].size();
24+
for(int i = 0; i < m; ++i)
25+
for(int j = 0; j < n; ++j)
26+
if(i == 0 || i == m-1 || j == 0 || j == n-1)
27+
bfs(board, i, j, m, n);
28+
for(int i = 0; i < m; ++i)
29+
for(int j = 0; j < n; ++j)
30+
board[i][j] = (board[i][j] == 'P') ? 'O' : 'X';
31+
}
32+
void bfs(vector<vector<char>> &board, int x, int y, int m, int n)
33+
{
34+
if(board[x][y] != 'O')
35+
return;
36+
queue<pair<int, int>> q;
37+
q.push(make_pair(x, y));
38+
while(!q.empty())
39+
{
40+
int i = q.front().first, j = q.front().second;
41+
q.pop();
42+
if(i < 0 || i >= m || j < 0 || j >= n)
43+
continue;
44+
if(board[i][j] != 'O')
45+
continue;
46+
board[i][j] = 'P';
47+
q.push(make_pair(i-1, j));
48+
q.push(make_pair(i+1, j));
49+
q.push(make_pair(i, j-1));
50+
q.push(make_pair(i, j+1));
51+
}
52+
}
53+
};

0 commit comments

Comments
 (0)