Skip to content

Commit 981bcd9

Browse files
author
applewjg
committed
Update
1 parent da824b5 commit 981bcd9

File tree

3 files changed

+72
-54
lines changed

3 files changed

+72
-54
lines changed

BalancedBinaryTree.h

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
Author: Annie Kim, [email protected]
2+
Author: Annie Kim, [email protected] : King, [email protected]
33
Date: Apr 10, 2013
4-
Update: Aug 4, 2013
4+
Update: Oct 07, 2014
55
Problem: Balanced Binary Tree
66
Difficulty: Easy
77
Source: http://leetcode.com/onlinejudge#question_110
@@ -25,19 +25,13 @@
2525
class Solution {
2626
public:
2727
bool isBalanced(TreeNode *root) {
28-
int height = 0;
29-
return isBalancedRe(root, height);
28+
return solve(root) != -1;
3029
}
31-
32-
bool isBalancedRe(TreeNode *root, int &height){
33-
if (!root) return true;
34-
35-
int leftHeight = 0, rightHeight = 0;
36-
if (!isBalancedRe(root->left, leftHeight)) return false;
37-
if (!isBalancedRe(root->right, rightHeight)) return false;
38-
if (abs(leftHeight-rightHeight) > 1) return false;
39-
40-
height = 1 + max(leftHeight, rightHeight);
41-
return true;
30+
int solve(TreeNode *root) {
31+
if (root == NULL) return 0;
32+
int left = solve(root->left);
33+
int right = solve(root->right);
34+
if (left == -1 || right == -1 || abs(left - right) > 1) return -1;
35+
return max(left,right) + 1;
4236
}
4337
};

DistinctSubsequences.h

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,53 @@
11
/*
2-
Author: Annie Kim, [email protected]
2+
Author: Annie Kim, [email protected] : King, [email protected]
33
Date: May 16, 2013
4+
Update: Oct 07, 2014
45
Problem: Distinct Subsequences
56
Difficulty: Easy
67
Source: http://leetcode.com/onlinejudge#question_115
78
Notes:
89
Given a string S and a string T, count the number of distinct subsequences of T in S.
9-
A subsequence of a string is a new string which is formed from the original string by deleting
10+
A subsequence of a string is a new string which is formed from the original string by deleting
1011
some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).
11-
12+
1213
Here is an example:
1314
S = "rabbbit", T = "rabbit"
1415
Return 3.
1516
1617
Solution: dp.
1718
*/
18-
1919
class Solution {
2020
public:
2121
int numDistinct(string S, string T) {
22-
int N = S.size(), M = T.size();
23-
int dp[M+1][N+1];
24-
dp[0][0] = 1;
25-
for (int j = 1; j <= N; ++j)
26-
dp[0][j] = 1;
27-
for (int i = 1; i <= M; ++i)
28-
dp[i][0] = 0;
29-
30-
for (int i = 1; i <= M; ++i)
31-
for (int j = 1; j <= N; ++j)
32-
if (S[j-1] == T[i-1])
33-
dp[i][j] = dp[i][j-1] + dp[i-1][j-1];
34-
else
35-
dp[i][j] = dp[i][j-1];
36-
22+
return numDistinct_2(S, T);
23+
}
24+
int numDistinct_1(string S, string T) {
25+
int M = S.size();
26+
int N = T.size();
27+
vector<vector<int> > dp(M+1, vector<int>(N+1));
28+
for (int j = 0; j <= N; ++j) {
29+
dp[0][j] = 0;
30+
}
31+
for (int i = 0; i <= M; ++i) {
32+
dp[i][0] = 1;
33+
}
34+
for (int i = 1; i <= M; ++i) {
35+
for (int j = 1; j <= N; ++j) {
36+
dp[i][j] = dp[i-1][j] + (S[i-1] == T[j-1] ? dp[i-1][j-1] : 0);
37+
}
38+
}
3739
return dp[M][N];
3840
}
41+
int numDistinct_2(string S, string T) {
42+
int M = S.size();
43+
int N = T.size();
44+
vector<int> dp(N + 1);
45+
dp[0] = 1;
46+
for (int i = 1; i <= M; ++i) {
47+
for (int j = N; j >=1; --j) {
48+
dp[j] = dp[j] + (S[i-1] == T[j-1] ? dp[j-1] : 0);
49+
}
50+
}
51+
return dp[N];
52+
}
3953
};

SymmetricTree.h

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
Author: Annie Kim, [email protected]
2+
Author: Annie Kim, [email protected] : King, [email protected]
33
Date: Apr 22, 2013
4-
Update: Jul 23, 2013
4+
Update: Oct 07, 2014
55
Problem: Symmetric Tree
66
Difficulty: Easy
77
Source: http://leetcode.com/onlinejudge#question_101
@@ -36,39 +36,49 @@
3636
class Solution {
3737
public:
3838
bool isSymmetric(TreeNode *root) {
39-
return isSymmetric_1(root);
39+
return isSymmetric_2(root);
4040
}
41-
4241
bool isSymmetric_1(TreeNode *root) {
43-
if (!root) return true;
44-
return isSymmetricRe(root->left, root->right);
42+
if (root == NULL) return true;
43+
return solve (root->left, root->right);
4544
}
46-
47-
bool isSymmetricRe(TreeNode *t1, TreeNode *t2)
48-
{
45+
bool solve(TreeNode * t1, TreeNode * t2) {
4946
if (!t1 && !t2) return true;
50-
if (!t1 || !t2 || t1->val != t2->val) return false;
51-
return isSymmetricRe(t1->left, t2->right) &&
52-
isSymmetricRe(t1->right, t2->left);
47+
if (!t1 && t2 || t1 && !t2 || t1->val != t2->val) return false;
48+
return solve(t1->left, t2->right) && solve(t1->right, t2->left);
5349
}
54-
5550
bool isSymmetric_2(TreeNode *root) {
56-
if (!root) return true;
51+
if (root == NULL) return true;
52+
stack<TreeNode *> s;
53+
s.push(root->left);
54+
s.push(root->right);
55+
while (!s.empty()) {
56+
TreeNode *t2 = s.top(); s.pop();
57+
TreeNode *t1 = s.top(); s.pop();
58+
if (!t1 && !t2) continue;
59+
if (!t1 && t2 || t1 && !t2 || t1->val != t2->val) return false;
60+
s.push(t1->right);
61+
s.push(t2->left);
62+
s.push(t1->left);
63+
s.push(t2->right);
64+
}
65+
return true;
66+
}
67+
bool isSymmetric_3(TreeNode *root) {
68+
if (root == NULL) return true;
5769
queue<TreeNode *> q;
5870
q.push(root->left);
5971
q.push(root->right);
60-
while (!q.empty())
61-
{
62-
TreeNode *t1 = q.front(); q.pop();
72+
while (!s.empty()) {
6373
TreeNode *t2 = q.front(); q.pop();
74+
TreeNode *t1 = q.front(); q.pop();
6475
if (!t1 && !t2) continue;
65-
if (!t1 || !t2 || t1->val != t2->val)
66-
return false;
76+
if (!t1 && t2 || t1 && !t2 || t1->val != t2->val) return false;
6777
q.push(t1->left);
6878
q.push(t2->right);
6979
q.push(t1->right);
7080
q.push(t2->left);
7181
}
7282
return true;
7383
}
74-
};
84+
};

0 commit comments

Comments
 (0)