Skip to content

Commit af881a4

Browse files
author
applewjg
committed
Update
1 parent d9192c0 commit af881a4

3 files changed

+100
-88
lines changed

Pascal'sTriangle.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ class Solution {
2323
public:
2424
vector<vector<int> > generate(int numRows) {
2525
vector<vector<int> > res(numRows);
26-
for (int i = 0; i < numRows; ++i)
26+
if (numRows < 1) return res;
27+
res[0].push_back(1);
28+
for (int i = 1; i < numRows; ++i)
2729
{
2830
res[i].push_back(1);
2931
for (int j = 1; j < i; ++j)
3032
res[i].push_back(res[i-1][j-1] + res[i-1][j]);
31-
if (i >= 1) res[i].push_back(1);
33+
res[i].push_back(1);
3234
}
3335
return res;
3436
}
35-
};
37+
};
Lines changed: 50 additions & 35 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: Sep 8, 2013
4+
Update: Oct 7, 2014
55
Problem: Populating Next Right Pointers in Each Node
66
Difficulty: Easy
77
Source: http://leetcode.com/onlinejudge#question_116
@@ -31,7 +31,7 @@
3131
/ \ / \
3232
4->5->6->7 -> NULL
3333
34-
Solution: 1. Iterative: Two 'while' loops: root->leaf and left->right.
34+
Solution: 1. Iterative: Two 'while' loops.
3535
2. Iterative: Queue. Use extra space.
3636
3. Recursive: DFS. Defect: Use extra stack space for recursion.
3737
*/
@@ -44,57 +44,72 @@
4444
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
4545
* };
4646
*/
47-
4847
class Solution {
4948
public:
5049
void connect(TreeLinkNode *root) {
51-
connect_1(root);
50+
connect_2(root);
5251
}
53-
5452
void connect_1(TreeLinkNode *root) {
55-
while (root)
56-
{
57-
TreeLinkNode *level = root;
58-
TreeLinkNode *last = NULL;
59-
while (level && level->left)
60-
{
61-
if (last) last->next = level->left;
62-
level->left->next = level->right;
63-
last = level->right;
64-
level = level->next;
53+
if (root == nullptr) return;
54+
TreeLinkNode *cur = root;
55+
TreeLinkNode dummy(-1);
56+
TreeLinkNode *pre = &dummy;
57+
while (cur) {
58+
pre = &dummy;
59+
pre->next = nullptr;
60+
while (cur) {
61+
if (cur->left) {
62+
pre->next = cur->left;
63+
pre = pre->next;
64+
}
65+
if (cur->right) {
66+
pre->next = cur->right;
67+
pre = pre->next;
68+
}
69+
cur = cur->next;
6570
}
66-
root = root->left;
71+
cur = dummy.next;
6772
}
6873
}
69-
7074
void connect_2(TreeLinkNode *root) {
71-
if (!root) return;
75+
if (root == NULL) return;
7276
queue<TreeLinkNode *> q;
7377
q.push(root);
7478
q.push(NULL);
7579
TreeLinkNode *last = NULL;
76-
while (true)
77-
{
80+
TreeLinkNode dummy(-1);
81+
TreeLinkNode *pre = &dummy;
82+
while (!q.empty()) {
7883
TreeLinkNode *node = q.front();
7984
q.pop();
80-
if (!node) {
81-
last = NULL;
82-
if (q.empty()) break;
83-
q.push(NULL);
85+
if (node == NULL) {
86+
if (dummy.next) q.push(NULL);
87+
pre = &dummy;
88+
pre->next = NULL;
8489
} else {
85-
if (last) last->next = node;
86-
last = node;
90+
pre->next = node;
91+
pre = pre->next;
8792
if (node->left) q.push(node->left);
8893
if (node->right) q.push(node->right);
8994
}
9095
}
9196
}
92-
93-
void connect_3(TreeLinkNode *node) {
94-
if (!node || !node->left) return;
95-
node->left->next = node->right;
96-
node->right->next = node->next ? node->next->left : NULL;
97-
connect_3(node->left);
98-
connect_3(node->right);
97+
void connect_3(TreeLinkNode *root) {
98+
if (root == nullptr) return;
99+
TreeLinkNode dummy(-1);
100+
TreeLinkNode *pre = &dummy;
101+
TreeLinkNode *cur = root;
102+
while (cur) {
103+
if (cur->left) {
104+
pre->next = cur->left;
105+
pre = pre->next;
106+
}
107+
if (cur->right) {
108+
pre->next = cur->right;
109+
pre = pre->next;
110+
}
111+
cur = cur->next;
112+
}
113+
connect(dummy.next);
99114
}
100-
};
115+
};
Lines changed: 45 additions & 50 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 23, 2013
4-
Update: Sep 8, 2013
4+
Update: Oct 7, 2014
55
Problem: Populating Next Right Pointers in Each Node II
66
Difficulty: Easy
77
Source: http://leetcode.com/onlinejudge#question_117
@@ -26,7 +26,7 @@
2626
2727
Solution: 1. iterative way with CONSTANT extra space.
2828
2. iterative way + queue. Contributed by SUN Mian(孙冕).
29-
3. tail recursive solution.
29+
3. recursive solution.
3030
*/
3131

3232
/**
@@ -40,74 +40,69 @@
4040
class Solution {
4141
public:
4242
void connect(TreeLinkNode *root) {
43-
connect_1(root);
43+
connect_2(root);
4444
}
45-
46-
// solution 1
4745
void connect_1(TreeLinkNode *root) {
46+
if (root == nullptr) return;
4847
TreeLinkNode *cur = root;
49-
while (cur)
50-
{
51-
TreeLinkNode *node = cur, *last = NULL;
52-
cur = NULL;
53-
while (node)
54-
{
55-
TreeLinkNode *left = node->left, *right = node->right;
56-
if (left || right) {
57-
if (last) last->next = left ? left : right;
58-
if (left) left->next = right;
59-
if (!cur) cur = left ? left : right;
60-
last = right ? right : left;
48+
TreeLinkNode dummy(-1);
49+
TreeLinkNode *pre = &dummy;
50+
while (cur) {
51+
pre = &dummy;
52+
pre->next = nullptr;
53+
while (cur) {
54+
if (cur->left) {
55+
pre->next = cur->left;
56+
pre = pre->next;
6157
}
62-
node = node->next;
58+
if (cur->right) {
59+
pre->next = cur->right;
60+
pre = pre->next;
61+
}
62+
cur = cur->next;
6363
}
64+
cur = dummy.next;
6465
}
6566
}
66-
67-
// solution 2
6867
void connect_2(TreeLinkNode *root) {
69-
if (!root) return;
68+
if (root == NULL) return;
7069
queue<TreeLinkNode *> q;
7170
q.push(root);
7271
q.push(NULL);
7372
TreeLinkNode *last = NULL;
74-
while (true)
75-
{
73+
TreeLinkNode dummy(-1);
74+
TreeLinkNode *pre = &dummy;
75+
while (!q.empty()) {
7676
TreeLinkNode *node = q.front();
7777
q.pop();
78-
if (!node) {
79-
last = NULL;
80-
if (q.empty()) break;
81-
q.push(NULL);
78+
if (node == NULL) {
79+
if (dummy.next) q.push(NULL);
80+
pre = &dummy;
81+
pre->next = NULL;
8282
} else {
83-
if (last) last->next = node;
84-
last = node;
83+
pre->next = node;
84+
pre = pre->next;
8585
if (node->left) q.push(node->left);
8686
if (node->right) q.push(node->right);
8787
}
8888
}
8989
}
90-
91-
// solution 3
9290
void connect_3(TreeLinkNode *root) {
93-
if (!root) return;
94-
TreeLinkNode *node = root;
95-
TreeLinkNode *last = NULL;
96-
TreeLinkNode *first = NULL;
97-
while (node)
98-
{
99-
if (node->left || node->right) {
100-
if (last)
101-
last->next = node->left ? node->left : node->right;
102-
if (node->left && node->right)
103-
node->left->next = node->right;
104-
if (!first)
105-
first = node->left ? node->left : node->right;
106-
last = node->right ? node->right : node->left;
107-
91+
if (root == nullptr) return;
92+
TreeLinkNode dummy(-1);
93+
TreeLinkNode *pre = &dummy;
94+
TreeLinkNode *cur = root;
95+
while (cur) {
96+
if (cur->left) {
97+
pre->next = cur->left;
98+
pre = pre->next;
99+
}
100+
if (cur->right) {
101+
pre->next = cur->right;
102+
pre = pre->next;
108103
}
109-
node = node->next;
104+
cur = cur->next;
110105
}
111-
connect_3(first);
106+
connect(dummy.next);
112107
}
113-
};
108+
};

0 commit comments

Comments
 (0)