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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 27 additions & 0 deletions
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+
};
Lines changed: 29 additions & 0 deletions
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+
};
Lines changed: 39 additions & 0 deletions
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

Lines changed: 13 additions & 0 deletions
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+
};
Lines changed: 18 additions & 0 deletions
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+
};
Lines changed: 22 additions & 0 deletions
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+
};
Lines changed: 32 additions & 0 deletions
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+
};
Lines changed: 16 additions & 0 deletions
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+
};
Lines changed: 35 additions & 0 deletions
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+
};

0 commit comments

Comments
 (0)