Skip to content

Commit a33cf3d

Browse files
committed
update
1 parent ec8528b commit a33cf3d

File tree

12 files changed

+55
-397
lines changed

12 files changed

+55
-397
lines changed

BalancedBinaryTree/BalancedBinaryTree.cpp

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -54,42 +54,26 @@ class Solution {
5454
}
5555
};
5656

57-
TreeNode * readNode(istringstream & is) {
58-
string str;
59-
if (is >> str) {
60-
if (str == "#") return NULL;
61-
return new TreeNode(stoi(str));
62-
}
63-
return NULL;
64-
}
65-
66-
TreeNode * fromString(string str) {
67-
str.erase(str.begin());
68-
str.pop_back();
69-
replace(begin(str), end(str), ',', ' ');
70-
istringstream is(str);
71-
TreeNode * root = readNode(is);
72-
queue<TreeNode *> qs;
73-
if (root != NULL) qs.push(root);
74-
while (!qs.empty()) {
75-
TreeNode * cur = qs.front();
76-
qs.pop();
77-
if (cur != NULL) {
78-
cur->left = readNode(is);
79-
if (cur->left != NULL) qs.push(cur->left);
80-
cur->right = readNode(is);
81-
if (cur->right != NULL) qs.push(cur->right);
82-
}
83-
}
84-
return root;
85-
}
86-
8757
int main() {
8858
Solution sol;
8959
TreeNode * p0;
9060

9161
{
92-
p0 = fromString("{1,2,2,3,3,3,3,4,4,4,4,4,4,#,#,5,5}");
62+
p0 = new TreeNode(1);
63+
p0->left = new TreeNode(2);
64+
p0->right = new TreeNode(2);
65+
p0->left->left = new TreeNode(3);
66+
p0->left->right = new TreeNode(3);
67+
p0->right->left = new TreeNode(3);
68+
p0->right->right = new TreeNode(3);
69+
p0->left->left->left = new TreeNode(4);
70+
p0->left->left->right = new TreeNode(4);
71+
p0->left->right->left = new TreeNode(4);
72+
p0->left->right->right = new TreeNode(4);
73+
p0->right->left->left = new TreeNode(4);
74+
p0->right->left->right = new TreeNode(4);
75+
p0->left->left->left->left = new TreeNode(5);
76+
p0->left->left->left->right = new TreeNode(5);
9377
cout << sol.isBalanced(p0) << endl;
9478
}
9579

BinaryTreeMaximumPathSum/BinaryTreeMaximumPathSum.cpp

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,61 +51,40 @@ class Solution {
5151
}
5252
};
5353

54-
TreeNode * readNode(istringstream & is) {
55-
string str;
56-
if (is >> str) {
57-
if (str == "#") return NULL;
58-
return new TreeNode(stoi(str));
59-
}
60-
return NULL;
61-
}
62-
63-
TreeNode * fromString(string str) {
64-
str.erase(str.begin());
65-
str.pop_back();
66-
replace(begin(str), end(str), ',', ' ');
67-
istringstream is(str);
68-
TreeNode * root = readNode(is);
69-
queue<TreeNode *> qs;
70-
if (root != NULL) qs.push(root);
71-
while (!qs.empty()) {
72-
TreeNode * cur = qs.front();
73-
qs.pop();
74-
if (cur != NULL) {
75-
cur->left = readNode(is);
76-
if (cur->left != NULL) qs.push(cur->left);
77-
cur->right = readNode(is);
78-
if (cur->right != NULL) qs.push(cur->right);
79-
}
80-
}
81-
return root;
82-
}
83-
8454
int main() {
8555
Solution sol;
8656
TreeNode *p0;
8757
int p1;
8858

8959
{
90-
p0 = fromString("{1,2,3}");
91-
p1 = sol.maxPathSum(p0);
60+
p0 = new TreeNode(1);
61+
p0->left = new TreeNode(2);
62+
p0->right = new TreeNode(3);
9263
cout << p1 << endl;
9364
}
9465

9566
{
96-
p0 = fromString("{-3}");
67+
p0 = new TreeNode(-3);
9768
p1 = sol.maxPathSum(p0);
9869
cout << p1 << endl;
9970
}
10071

10172
{
102-
p0 = fromString("{}");
73+
p0 = NULL;
10374
p1 = sol.maxPathSum(p0);
10475
cout << p1 << endl;
10576
}
10677

10778
{
108-
p0 = fromString("{9,6,-3,#,#,-6,2,#,#,2,#,-6,-6,-6}");
79+
p0 = new TreeNode(9);
80+
p0->left = new TreeNode(6);
81+
p0->right = new TreeNode(-3);
82+
p0->right->left = new TreeNode(-6);
83+
p0->right->right = new TreeNode(2);
84+
p0->right->right->left = new TreeNode(2);
85+
p0->right->right->left->left = new TreeNode(-6);
86+
p0->right->right->left->right = new TreeNode(-6);
87+
p0->right->right->left->left->left = new TreeNode(-6);
10988
p1 = sol.maxPathSum(p0);
11089
cout << p1 << endl;
11190
}

ConstructBinaryTreefromInorderandPostorderTraversal/ConstructBinaryTreefromInorderandPostorderTraversal.cpp

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -61,42 +61,6 @@ class Solution {
6161
}
6262
};
6363

64-
string toString(TreeNode * root) {
65-
ostringstream os;
66-
queue<TreeNode *> cq, nq;
67-
cq.push(root);
68-
while (!cq.empty()) {
69-
vector<int> path;
70-
while (!cq.empty()) {
71-
TreeNode * cur = cq.front();
72-
cq.pop();
73-
if (cur == NULL) {
74-
os << "# ";
75-
}
76-
else {
77-
os << cur->val << " ";
78-
nq.push(cur->left);
79-
nq.push(cur->right);
80-
}
81-
}
82-
swap(cq, nq);
83-
}
84-
string res = os.str();
85-
res.pop_back();
86-
return res;
87-
}
88-
8964
int main() {
90-
Solution sol;
91-
vector<int> p0;
92-
vector<int> p1;
93-
94-
{
95-
p0 = { 1, 3, 2 };
96-
p1 = { 3, 2, 1 };
97-
auto p2 = sol.buildTree(p0, p1);
98-
cout << toString(p2) << endl;
99-
}
100-
10165
return 0;
10266
}

ConstructBinaryTreefromPreorderandInorderTraversal/ConstructBinaryTreefromPreorderandInorderTraversal.cpp

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <iostream>
1414
#include <vector>
1515
#include <algorithm>
16+
1617
using namespace std;
1718

1819
/**
@@ -61,42 +62,6 @@ class Solution {
6162
}
6263
};
6364

64-
string toString(TreeNode * root) {
65-
ostringstream os;
66-
queue<TreeNode *> cq, nq;
67-
cq.push(root);
68-
while (!cq.empty()) {
69-
vector<int> path;
70-
while (!cq.empty()) {
71-
TreeNode * cur = cq.front();
72-
cq.pop();
73-
if (cur == NULL) {
74-
os << "# ";
75-
}
76-
else {
77-
os << cur->val << " ";
78-
nq.push(cur->left);
79-
nq.push(cur->right);
80-
}
81-
}
82-
swap(cq, nq);
83-
}
84-
string res = os.str();
85-
res.pop_back();
86-
return res;
87-
}
88-
8965
int main() {
90-
Solution sol;
91-
vector<int> p0;
92-
vector<int> p1;
93-
94-
{
95-
p0 = { 1, 2, 3 };
96-
p1 = { 1, 3, 2 };
97-
auto p2 = sol.buildTree(p0, p1);
98-
cout << toString(p2) << endl;
99-
}
100-
10166
return 0;
10267
}

ConvertSortedArraytoBinarySearchTree/ConvertSortedArraytoBinarySearchTree.cpp

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -50,39 +50,6 @@ class Solution {
5050
}
5151
};
5252

53-
string toString(TreeNode * root) {
54-
ostringstream os;
55-
queue<TreeNode *> cq, nq;
56-
cq.push(root);
57-
while (!cq.empty()) {
58-
vector<int> path;
59-
while (!cq.empty()) {
60-
TreeNode * cur = cq.front();
61-
cq.pop();
62-
if (cur == NULL) {
63-
os << "#,";
64-
}
65-
else {
66-
os << cur->val << ",";
67-
nq.push(cur->left);
68-
nq.push(cur->right);
69-
}
70-
}
71-
swap(cq, nq);
72-
}
73-
string res = os.str();
74-
res.pop_back();
75-
return "{" + res + "}";
76-
}
77-
7853
int main() {
79-
Solution sol;
80-
vector<int> p0;
81-
82-
{
83-
p0 = { 3, 5, 8 };
84-
auto p1 = sol.sortedArrayToBST(p0);
85-
cout << toString(p1) << endl;
86-
}
8754
return 0;
8855
}

ConvertSortedListtoBinarySearchTree/ConvertSortedListtoBinarySearchTree.cpp

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -81,41 +81,6 @@ class Solution {
8181
};
8282
};
8383

84-
string toString(TreeNode * root) {
85-
ostringstream os;
86-
queue<TreeNode *> cq, nq;
87-
cq.push(root);
88-
while (!cq.empty()) {
89-
vector<int> path;
90-
while (!cq.empty()) {
91-
TreeNode * cur = cq.front();
92-
cq.pop();
93-
if (cur == NULL) {
94-
os << "# ";
95-
}
96-
else {
97-
os << cur->val << " ";
98-
nq.push(cur->left);
99-
nq.push(cur->right);
100-
}
101-
}
102-
swap(cq, nq);
103-
}
104-
string res = os.str();
105-
res.pop_back();
106-
return res;
107-
}
108-
10984
int main() {
110-
Solution sol;
111-
ListNode * p0;
112-
113-
{
114-
p0 = new ListNode(3);
115-
p0->next = new ListNode(5);
116-
p0->next->next = new ListNode(8);
117-
auto p1 = sol.sortedListToBST(p0);
118-
cout << toString(p1) << endl;
119-
}
12085
return 0;
12186
}

FlattenBinaryTreetoLinkedList/FlattenBinaryTreetoLinkedList.cpp

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -96,43 +96,6 @@ class Solution {
9696
}
9797
};
9898

99-
string toString(TreeNode * root) {
100-
ostringstream os;
101-
queue<TreeNode *> cq, nq;
102-
cq.push(root);
103-
while (!cq.empty()) {
104-
vector<int> path;
105-
while (!cq.empty()) {
106-
TreeNode * cur = cq.front();
107-
cq.pop();
108-
if (cur == NULL) {
109-
os << "# ";
110-
}
111-
else {
112-
os << cur->val << " ";
113-
nq.push(cur->left);
114-
nq.push(cur->right);
115-
}
116-
}
117-
swap(cq, nq);
118-
}
119-
string res = os.str();
120-
res.pop_back();
121-
return "{" + res + "}";
122-
}
123-
12499
int main() {
125-
Solution sol;
126-
TreeNode * p0;
127-
128-
{
129-
p0 = new TreeNode(1);
130-
p0->left = new TreeNode(2);
131-
p0->right = new TreeNode(5);
132-
p0->left->left = new TreeNode(3);
133-
p0->left->right = new TreeNode(4);
134-
p0->right->right = new TreeNode(6);
135-
sol.flatten(p0);
136-
cout << toString(p0) << endl;
137-
}
100+
return 0;
138101
}

0 commit comments

Comments
 (0)