Skip to content

Commit bccc2ba

Browse files
committed
update
1 parent 7283452 commit bccc2ba

File tree

10 files changed

+192
-241
lines changed

10 files changed

+192
-241
lines changed

BinaryTreeInorderTraversal/BinaryTreeInorderTraversal.cpp

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,53 +39,36 @@ class Solution {
3939
return inorderTraversal2(root);
4040
}
4141

42-
vector<int> inorderTraversal1(TreeNode * root) {
42+
vector<int> inorderTraversal1(TreeNode *root) {
4343
vector<int> res;
44-
inorderTraversalHelper1(root, res);
45-
return res;
46-
}
47-
48-
void inorderTraversalHelper1(TreeNode * node, vector<int> & res) {
49-
if (node == NULL) return;
50-
inorderTraversalHelper1(node->left, res);
51-
res.push_back(node->val);
52-
inorderTraversalHelper1(node->right, res);
53-
}
54-
55-
vector<int> inorderTraversal2(TreeNode * root) {
56-
vector<int> res;
57-
stack<TreeNode*> stk;
58-
TreeNode * cur = root;
59-
while (cur != NULL) stk.push(cur), cur = cur->left;
44+
stack<TreeNode *> stk;
45+
for (; root != NULL; root = root->left) stk.push(root);
6046
while (!stk.empty()) {
61-
cur = stk.top(), stk.pop();
62-
res.push_back(cur->val);
63-
cur = cur->right;
64-
while (cur != NULL) stk.push(cur), cur = cur->left;
47+
root = stk.top(), stk.pop(), res.push_back(root->val);
48+
for (root = root->right; root != NULL; root = root->left) stk.push(root);
6549
}
6650
return res;
6751
}
6852

69-
vector<int> inorderTraversal3(TreeNode * root) {
53+
vector<int> inorderTraversal2(TreeNode * root) {
7054
vector<int> res;
71-
TreeNode * cur = root;
72-
while (cur != NULL) {
73-
if (cur->left != NULL) {
74-
TreeNode * pre = cur->left;
75-
while (pre->right != NULL && pre->right != cur) pre = pre->right;
55+
while (root != NULL) {
56+
if (root->left != NULL) {
57+
auto pre = root->left;
58+
while (pre->right != NULL && pre->right != root) pre = pre->right;
7659
if (pre->right == NULL) {
77-
pre->right = cur;
78-
cur = cur->left;
60+
pre->right = root;
61+
root = root->left;
7962
}
8063
else {
8164
pre->right = NULL;
82-
res.push_back(cur->val);
83-
cur = cur->right;
65+
res.push_back(root->val);
66+
root = root->right;
8467
}
8568
}
8669
else {
87-
res.push_back(cur->val);
88-
cur = cur->right;
70+
res.push_back(root->val);
71+
root = root->right;
8972
}
9073
}
9174
return res;

BinaryTreePostorderTraversal/BinaryTreePostorderTraversal.cpp

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,42 +35,14 @@ struct TreeNode {
3535

3636
class Solution {
3737
public:
38-
vector<int> postorderTraversal(TreeNode* root) {
39-
return postorderTraversal1(root);
40-
}
41-
42-
vector<int> postorderTraversal1(TreeNode* root) {
43-
vector<int> res;
44-
postorderTraversalHelper1(root, res);
45-
return res;
46-
}
47-
48-
void postorderTraversalHelper1(TreeNode* node, vector<int> & res) {
49-
if (node == NULL) return;
50-
postorderTraversalHelper1(node->left, res);
51-
postorderTraversalHelper1(node->right, res);
52-
res.push_back(node->val);
53-
}
54-
55-
vector<int> postorderTraversal2(TreeNode *root) {
38+
vector<int> postorderTraversal(TreeNode *root) {
5639
vector<int> res;
57-
stack<TreeNode*> stk;
58-
TreeNode * cur = root;
59-
while (cur != NULL) {
60-
res.push_back(cur->val);
61-
stk.push(cur);
62-
cur = cur->right;
63-
}
40+
stack<TreeNode *> stk;
41+
for (; root != NULL; root = root->right) res.push_back(root->val), stk.push(root);
6442
while (!stk.empty()) {
65-
cur = stk.top(), stk.pop();
66-
cur = cur->left;
67-
while (cur != NULL) {
68-
res.push_back(cur->val);
69-
stk.push(cur);
70-
cur = cur->right;
71-
}
43+
root = stk.top()->left, stk.pop();
44+
for (; root != NULL; root = root->right) res.push_back(root->val), stk.push(root);
7245
}
73-
7446
reverse(begin(res), end(res));
7547
return res;
7648
}

BinaryTreePreorderTraversal/BinaryTreePreorderTraversal.cpp

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,39 +37,12 @@ struct TreeNode {
3737
class Solution {
3838
public:
3939
vector<int> preorderTraversal(TreeNode *root) {
40-
return preorderTraversal2(root);
41-
}
42-
43-
vector<int> preorderTraversal1(TreeNode * root) {
4440
vector<int> res;
45-
preorderTraversalHelper1(root, res);
46-
return res;
47-
}
48-
49-
void preorderTraversalHelper1(TreeNode * node, vector<int> & res) {
50-
if (node == NULL) return;
51-
res.push_back(node->val);
52-
preorderTraversalHelper1(node->left, res);
53-
preorderTraversalHelper1(node->right, res);
54-
}
55-
56-
vector<int> preorderTraversal2(TreeNode * root) {
57-
vector<int> res;
58-
stack<TreeNode*> stk;
59-
TreeNode * cur = root;
60-
while (cur != NULL) {
61-
res.push_back(cur->val);
62-
stk.push(cur);
63-
cur = cur->left;
64-
}
41+
stack<TreeNode *> stk;
42+
for (; root != NULL; root = root->left) res.push_back(root->val), stk.push(root);
6543
while (!stk.empty()) {
66-
cur = stk.top(), stk.pop();
67-
cur = cur->right;
68-
while (cur != NULL) {
69-
res.push_back(cur->val);
70-
stk.push(cur);
71-
cur = cur->left;
72-
}
44+
root = stk.top()->right, stk.pop();
45+
for (; root != NULL; root = root->left) res.push_back(root->val), stk.push(root);
7346
}
7447
return res;
7548
}

BinaryTreeZigzagLevelOrderTraversal/BinaryTreeZigzagLevelOrderTraversal.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,27 @@ class Solution {
4141
public:
4242
vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
4343
vector<vector<int> > res;
44-
stack<TreeNode *> cs, ns;
45-
if (root != NULL) cs.push(root);
44+
vector<stack<TreeNode *> > stk(2, stack<TreeNode *>());
4645
bool order = true;
47-
while (!cs.empty()) {
46+
int pre = 0, cur = 1;
47+
if (root != NULL) stk[pre].push(root);
48+
while (!stk[pre].empty()) {
4849
vector<int> path;
49-
while (!cs.empty()) {
50-
TreeNode * cur = cs.top();
51-
cs.pop();
52-
path.push_back(cur->val);
50+
while (!stk[pre].empty()) {
51+
root = stk[pre].top(), stk[pre].pop();
52+
path.push_back(root->val);
5353
if (order) {
54-
if (cur->left != NULL) ns.push(cur->left);
55-
if (cur->right != NULL) ns.push(cur->right);
54+
if (root->left != NULL) stk[cur].push(root->left);
55+
if (root->right != NULL) stk[cur].push(root->right);
5656
}
5757
else {
58-
if (cur->right != NULL) ns.push(cur->right);
59-
if (cur->left != NULL) ns.push(cur->left);
58+
if (root->right != NULL) stk[cur].push(root->right);
59+
if (root->left != NULL) stk[cur].push(root->left);
6060
}
6161
}
62-
order = !order;
6362
res.push_back(path);
64-
swap(cs, ns);
63+
pre = cur, cur = !cur;
64+
order = !order;
6565
}
6666
return res;
6767
}

PopulatingNextRightPointersinEachNode/PopulatingNextRightPointersinEachNode.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,28 @@ struct TreeLinkNode
5454
class Solution {
5555
public:
5656
void connect(TreeLinkNode *root) {
57+
return connect3(root);
58+
}
59+
60+
void connect1(TreeLinkNode *root) {
61+
if (root == NULL) return;
62+
if (root->left != NULL) root->left->next = root->right;
63+
if (root->right != NULL) root->right->next = (root->next == NULL) ? NULL : root->next->left;
64+
connect1(root->left);
65+
connect1(root->right);
66+
}
67+
68+
void connect2(TreeLinkNode * root) {
69+
queue<TreeLinkNode *> qs;
70+
if (root != NULL) qs.push(root);
71+
while (!qs.empty()) {
72+
root = qs.front(), qs.pop();
73+
if (root->left != NULL) root->left->next = root->right, qs.push(root->left);
74+
if (root->right != NULL) root->right->next = (root->next == NULL) ? NULL : root->next->left, qs.push(root->right);
75+
}
76+
}
77+
78+
void connect3(TreeLinkNode * root) {
5779
while (root != NULL) {
5880
TreeLinkNode * cur = root;
5981
while (cur != NULL) {

PopulatingNextRightPointersinEachNodeII/PopulatingNextRightPointersinEachNodeII.cpp

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,38 @@ struct TreeLinkNode
4444
class Solution {
4545
public:
4646
void connect(TreeLinkNode *root) {
47-
while (root != NULL) {
48-
TreeLinkNode * cur = root;
49-
while (cur != NULL) {
50-
if (cur->left != NULL && cur->right != NULL) cur->left->next = cur->right;
51-
TreeLinkNode * next = cur->next;
52-
while (next != NULL && next->left == NULL && next->right == NULL) next = next->next;
53-
if (next == NULL) break;
54-
TreeLinkNode * child = (next->left != NULL) ? next->left : next->right;
55-
if (cur->right != NULL) {
56-
cur->right->next = child;
57-
}
58-
else if (cur->left != NULL) {
59-
cur->left->next = child;
60-
}
61-
cur = next;
47+
return connect2(root);
48+
}
49+
50+
void connect2(TreeLinkNode * root) {
51+
queue<TreeLinkNode *> qs;
52+
if (root != NULL) qs.push(root), qs.push(NULL);
53+
while (!qs.empty()) {
54+
root = qs.front(), qs.pop();
55+
if (root == NULL) {
56+
if (!qs.empty()) qs.push(NULL);
57+
}
58+
else {
59+
root->next = qs.front();
60+
if (root->left != NULL) qs.push(root->left);
61+
if (root->right != NULL) qs.push(root->right);
6262
}
63+
}
64+
}
6365

66+
void connect3(TreeLinkNode * root) {
67+
while (root != NULL) {
68+
TreeLinkNode * curNode = root;
69+
while (curNode != NULL) {
70+
if (curNode->left != NULL && curNode->right != NULL) curNode->left->next = curNode->right;
71+
TreeLinkNode * nextNode = curNode->next;
72+
while (nextNode != NULL && nextNode->left == NULL && nextNode->right == NULL) nextNode = nextNode->next;
73+
if (nextNode == NULL) break;
74+
TreeLinkNode * childNode = (nextNode->left != NULL) ? nextNode->left : nextNode->right;
75+
if (curNode->right != NULL) curNode->right->next = childNode;
76+
else if (curNode->left != NULL) curNode->left->next = childNode;
77+
curNode = nextNode;
78+
}
6479
while (root != NULL && root->left == NULL && root->right == NULL) root = root->next;
6580
if (root != NULL) root = (root->left != NULL) ? root->left : root->right;
6681
}

RecoverBinarySearchTree/RecoverBinarySearchTree.cpp

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,50 +48,46 @@ class Solution {
4848
void recoverTree2(TreeNode *root) {
4949
TreeNode * pre = NULL, *first = NULL, *second = NULL;
5050
stack<TreeNode *> stk;
51-
TreeNode * cur = root;
52-
while (cur != NULL) stk.push(cur), cur = cur->left;
51+
for (; root != NULL; root = root->left) stk.push(root);
5352
while (!stk.empty()) {
54-
cur = stk.top(), stk.pop();
55-
if (pre != NULL && pre->val > cur->val) {
53+
root = stk.top(), stk.pop();
54+
if (pre != NULL && pre->val > root->val) {
5655
if (first == NULL) first = pre;
57-
second = cur;
56+
second = root;
5857
}
59-
pre = cur;
60-
cur = cur->right;
61-
while (cur != NULL) stk.push(cur), cur = cur->left;
58+
for (root = root->right; root != NULL; root = root->left) stk.push(root);
6259
}
63-
6460
swap(first->val, second->val);
6561
}
6662

6763
void recoverTree3(TreeNode *root) {
68-
TreeNode * pre = NULL, *first = NULL, *second = NULL;
69-
TreeNode * cur = root;
70-
while (cur != NULL) {
71-
if (cur->left != NULL) {
72-
TreeNode * p = cur->left;
73-
while (p->right != NULL && p->right != cur) p = p->right;
74-
if (p->right == NULL) {
75-
p->right = cur;
76-
cur = cur->left;
64+
TreeNode * last = NULL, *first = NULL, *second = NULL;
65+
66+
while (root != NULL) {
67+
if (root->left != NULL) {
68+
TreeNode * pre = root->left;
69+
while (pre->right != NULL && pre->right != root) pre = pre->right;
70+
if (pre->right == NULL) {
71+
pre->right = root;
72+
root = root->left;
7773
}
7874
else {
79-
p->right = NULL;
80-
if (pre != NULL && pre->val > cur->val) {
81-
if (first == NULL) first = pre;
82-
second = cur;
75+
pre->right = NULL;
76+
if (last != NULL && last->val > root->val) {
77+
if (first == NULL) first = last;
78+
second = root;
8379
}
84-
pre = cur;
85-
cur = cur->right;
80+
last = root;
81+
root = root->right;
8682
}
8783
}
8884
else {
89-
if (pre != NULL && pre->val > cur->val) {
90-
if (first == NULL) first = pre;
91-
second = cur;
85+
if (last != NULL && last->val > root->val) {
86+
if (first == NULL) first = last;
87+
second = root;
9288
}
93-
pre = cur;
94-
cur = cur->right;
89+
last = root;
90+
root = root->right;
9591
}
9692
}
9793

0 commit comments

Comments
 (0)