Skip to content

Commit 5ad09a3

Browse files
author
bintan
committed
第一次提交
1 parent 5bc80d7 commit 5ad09a3

File tree

88 files changed

+2538
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+2538
-0
lines changed

100. Same Tree.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include"Inc.h"
2+
class Solution {
3+
public:
4+
bool isSameTree(TreeNode* p, TreeNode* q) {
5+
if (!p&&!q) return true;
6+
else if (p&&q) {
7+
if (p->val != q->val) return false;
8+
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
9+
}
10+
else return false;
11+
}
12+
};

101. Symmetric Tree.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include"Inc.h"
2+
class Solution {
3+
public:
4+
bool isSymmetric(TreeNode* root) {
5+
if (!root) return true;
6+
queue<TreeNode*> Q1;
7+
queue<TreeNode*> Q2;
8+
TreeNode* p, *q;
9+
Q1.push(root);
10+
Q2.push(root);
11+
while (!Q1.empty() && !Q2.empty()) {
12+
p = Q1.front(); Q1.pop();
13+
q = Q2.front(); Q2.pop();
14+
if (p&&q) {
15+
if (p->val != q->val) return false;
16+
else {
17+
Q1.push(p->left);
18+
Q1.push(p->right);
19+
Q2.push(q->right);
20+
Q2.push(q->left);
21+
}
22+
}
23+
if ((!p&&q) || (!q&&p)) return false;
24+
}
25+
return true;
26+
}
27+
};
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include"Inc.h"
2+
class Solution {
3+
public:
4+
vector<vector<int>> levelOrder(TreeNode* root) {
5+
vector<vector<int>> ret;
6+
vector<int> curr;
7+
if (!root) return ret;
8+
int front = -1, rear = -1;
9+
int last = 0, level = 0;
10+
TreeNode* Q[10000], *p;
11+
p = root;
12+
Q[++rear] = root;
13+
while (front < rear) {
14+
p = Q[++front];
15+
if (p->left) Q[++rear] = p->left;
16+
if (p->right) Q[++rear] = p->right;
17+
if (front < last)
18+
curr.push_back(p->val);
19+
if (front == last) {
20+
curr.push_back(p->val);
21+
ret.push_back(curr);
22+
last = rear;
23+
curr.clear();
24+
}
25+
26+
}
27+
return ret;
28+
}
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include"Inc.h"
2+
class Solution {
3+
public:
4+
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
5+
vector<vector<int>> ret;
6+
vector<int> curr;
7+
if (!root) return ret;
8+
int front = -1, rear = -1;
9+
int last = 0, level = 0;
10+
TreeNode* Q[10000], *p;
11+
p = root;
12+
Q[++rear] = root;
13+
int flag = 0;
14+
while (front < rear) {
15+
p = Q[++front];
16+
if (p->left) Q[++rear] = p->left;
17+
if (p->right) Q[++rear] = p->right;
18+
if (front < last) {
19+
if (flag == 0) curr.push_back(p->val);
20+
else curr.insert(curr.begin(), p->val);
21+
}
22+
if (front == last) {
23+
if (flag == 0) {
24+
curr.push_back(p->val);
25+
flag = 1;
26+
}
27+
else {
28+
curr.insert(curr.begin(), p->val);
29+
flag = 0;
30+
}
31+
ret.push_back(curr);
32+
last = rear;
33+
curr.clear();
34+
}
35+
36+
}
37+
return ret;
38+
}
39+
};

104. Maximum Depth of Binary Tree.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include"Inc.h"
2+
class Solution {
3+
public:
4+
int maxDepth(TreeNode* root) {
5+
if (!root) return 0;
6+
else {
7+
int left = maxDepth(root->left);
8+
int right = maxDepth(root->right);
9+
return (left > right ? left : right) + 1;
10+
}
11+
}
12+
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include"Inc.h"
2+
class Solution {
3+
public:
4+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
5+
if (!preorder.size() || !inorder.size()) return NULL;
6+
return build(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1);
7+
8+
}
9+
TreeNode* build(vector<int>& preorder, vector<int>& inorder, int plow, int phigh, int ilow, int ihigh) {
10+
if (plow > phigh || ilow > ihigh) return NULL;
11+
TreeNode* root = new TreeNode(preorder[plow]);
12+
int i = ilow;
13+
for (; i <= ihigh&&inorder[i] != preorder[plow]; i++);
14+
root->left = build(preorder, inorder, plow + 1, plow + i - ilow, ilow, i - 1);
15+
root->right = build(preorder, inorder, plow + i - ilow + 1, phigh, i + 1, ihigh);
16+
return root;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include"Inc.h"
2+
class Solution {
3+
public:
4+
TreeNode* build(vector<int>& inorder, vector<int>& postorder, int ilow, int ihigh, int plow, int phigh) {
5+
if (ilow > ihigh || plow > phigh) return NULL;
6+
TreeNode* root = new TreeNode(postorder[phigh]);
7+
int i = ilow;
8+
for (i = ilow; i <= ihigh&&inorder[i] != postorder[phigh]; i++);
9+
int pi = i - ilow;
10+
root->left = build(inorder, postorder, ilow, i - 1, plow, plow + pi - 1);
11+
root->right = build(inorder, postorder, i + 1, ihigh, plow + pi, phigh - 1);
12+
return root;
13+
}
14+
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
15+
if (!inorder.size() || !postorder.size()) return NULL;
16+
int lenght = inorder.size();
17+
18+
return build(inorder, postorder, 0, lenght - 1, 0, lenght - 1);
19+
20+
21+
}
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include"Inc.h"
2+
class Solution {
3+
public:
4+
vector<vector<int>> levelOrderBottom(TreeNode* root) {
5+
vector<vector<int>> ret;
6+
vector<vector<int>> ret2;
7+
vector<int> curr;
8+
if (!root) return ret2;
9+
int front = -1, rear = -1;
10+
int last = 0, level = 0;
11+
TreeNode* Q[10000], *p;
12+
p = root;
13+
Q[++rear] = root;
14+
while (front < rear) {
15+
p = Q[++front];
16+
if (p->left) Q[++rear] = p->left;
17+
if (p->right) Q[++rear] = p->right;
18+
if (front < last)
19+
curr.push_back(p->val);
20+
if (front == last) {
21+
curr.push_back(p->val);
22+
ret.push_back(curr);
23+
last = rear;
24+
curr.clear();
25+
}
26+
27+
}
28+
for (int i = ret.size() - 1; i >= 0; i--)
29+
ret2.push_back(ret[i]);
30+
return ret2;
31+
}
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include"Inc.h"
2+
class Solution {
3+
public:
4+
TreeNode* build(vector<int>& nums, int low, int high) {
5+
if (low > high) return NULL;
6+
int mid = (low + high) / 2;
7+
TreeNode* node = new TreeNode(nums[mid]);
8+
node->left = build(nums, low, mid - 1);
9+
node->right = build(nums, mid + 1, high);
10+
return node;
11+
}
12+
TreeNode* sortedArrayToBST(vector<int>& nums) {
13+
if (!nums.size()) return NULL;
14+
return build(nums, 0, nums.size() - 1);
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include<vector>
2+
using namespace std;
3+
struct ListNode {
4+
int val;
5+
ListNode *next;
6+
ListNode(int x) : val(x), next(NULL) {}
7+
};
8+
9+
10+
struct TreeNode {
11+
int val;
12+
TreeNode *left;
13+
TreeNode *right;
14+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
15+
};
16+
class Solution {
17+
public:
18+
TreeNode* build(ListNode* begin, ListNode* end) {
19+
if (begin == end) return NULL;
20+
ListNode* fast = begin;
21+
ListNode* slow = begin;
22+
while (fast != end&&fast->next != end) {
23+
fast = fast->next->next;
24+
slow = slow->next;
25+
}
26+
TreeNode* root = new TreeNode(slow->val);
27+
root->left = build(begin, slow);
28+
root->right = build(slow->next, end);
29+
return root;
30+
}
31+
TreeNode* sortedListToBST(ListNode* head) {
32+
if (!head) return NULL;
33+
return build(head, NULL);
34+
}
35+
};

11. Container With Most Water.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include<vector>
2+
#include<stack>
3+
#include<algorithm>
4+
using namespace std;
5+
class Solution {
6+
public:
7+
int maxArea(vector<int>& height) {
8+
if (height.empty()) return 0;
9+
int i = 0, j = height.size() - 1;
10+
int ret = 0, curr = 0;
11+
while (i < j) {
12+
curr = min(height[i], height[j])*(j - i);
13+
ret = max(ret, curr);
14+
if (height[i] < height[j]) {
15+
int left = i;
16+
i++;
17+
while (height[i] < height[left]) i++;
18+
}
19+
else {
20+
int right = j;
21+
j--;
22+
while (height[j] < height[right]) j--;
23+
}
24+
}
25+
return ret;
26+
}
27+
};

110. Balanced Binary Tree.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include"Inc.h"
2+
class Solution {
3+
public:
4+
int CheckHeight(TreeNode* p) {
5+
if (!p) return 0;
6+
int leftHeight = CheckHeight(p->left);
7+
if (leftHeight == -1) return -1;
8+
int rightHeight = CheckHeight(p->right);
9+
if (rightHeight == -1) return -1;
10+
int heightDiff = abs(leftHeight - rightHeight);
11+
if (heightDiff > 1) return -1;
12+
else return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
13+
}
14+
bool isBalanced(TreeNode* root) {
15+
if (!root) return true;
16+
if (CheckHeight(root) == -1) return false;
17+
else return true;
18+
19+
}
20+
};

111. Minimum Depth of Binary Tree.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include"Inc.h"
2+
class Solution {
3+
public:
4+
int minDepth(TreeNode* root) {
5+
if (!root) return 0;
6+
if (!root->left) return minDepth(root->right) + 1;
7+
else if (!root->right) return minDepth(root->left) + 1;
8+
else {
9+
int left = minDepth(root->left);
10+
int right = minDepth(root->right);
11+
return (left < right ? left : right) + 1;
12+
}
13+
}
14+
};

112. Path Sum.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include"Inc.h"
2+
class Solution {
3+
public:
4+
bool CheckPath(stack<TreeNode*> s, int sum) {
5+
if (s.empty() && sum != 0) return true;
6+
int path = 0;
7+
while (!s.empty()) {
8+
path += s.top()->val;
9+
s.pop();
10+
}
11+
if (path == sum) return true;
12+
else return false;
13+
}
14+
bool hasPathSum(TreeNode* root, int sum) {
15+
stack<TreeNode*> poststack;
16+
TreeNode* p, *pre;
17+
p = root;
18+
pre = NULL;
19+
while (p) {
20+
poststack.push(p);
21+
p = p->left;
22+
}
23+
while (!poststack.empty()) {
24+
p = poststack.top();
25+
if (!p->right || p->right == pre) {
26+
if (!p->left&&!p->right) {
27+
if (CheckPath(poststack, sum))
28+
return true;
29+
}
30+
pre = p;
31+
poststack.pop();
32+
}
33+
else {
34+
p = p->right;
35+
while (p) {
36+
poststack.push(p);
37+
p = p->left;
38+
}
39+
}
40+
}
41+
return false;
42+
}
43+
};
44+

0 commit comments

Comments
 (0)