27. 二叉树的镜像
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
if(root==nullptr) return nullptr;
TreeNode* temp = root->left;
root->left = mirrorTree(root->right);
root->right = mirrorTree(temp);
return root;
}
};
28. 对称的二叉树
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root==nullptr) return true;
return dfs(root->left,root->right);
}
bool dfs(TreeNode* left,TreeNode* right){
if(left == nullptr && right ==nullptr) return true;
if(left ==nullptr || right==nullptr) return false;
if(left->val != right->val) return false;
return dfs(left->left,right->right) && dfs(left->right,right->left);
}
};
29. 顺时针打印矩阵
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> res;
if(matrix.size()==0 || matrix[0].size()==0) return res;
int m = matrix.size(),n=matrix[0].size();
int max = m*n;
int left = 0,right = matrix[0].size()-1,up = 0,down = matrix.size()-1,num=0;
while(1){
for(int i=left;i<=right;i++){
res.push_back(matrix[up][i]);
num++;
if(num == max) return res;
}
for(int i=up+1;i<=down-1;i++){
res.push_back(matrix[i][right]);
num++;
if(num == max) return res;
}
for(int i=right;i>=left;i--){
res.push_back(matrix[down][i]);
num++;
if(num == max) return res;
}
for(int i=down-1;i>=up+1;i--){
res.push_back(matrix[i][left]);
num++;
if(num == max) return res;
}
left++;
right--;
up++;
down--;
}
return res;
}
};
30. 包含min函数的栈
class MinStack {
public:
stack<int> s1;
stack<int> s2;
MinStack() {
}
void push(int x) {
if(s1.empty()) s2.push(x);
else {
if(s2.top() >x) s2.push(x);
else s2.push(s2.top());
}
s1.push(x);
}
void pop() {
s1.pop();
s2.pop();
}
int top() {
return s1.top();
}
int min() {
return s2.top();
}
};
31. 栈的压入、弹出序列
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> st;
int index=0;
for(int i:pushed){
st.push(i);
while(!st.empty() && st.top()==popped[index]){
st.pop();
index++;
}
}
return st.empty();
}
};
32 - I. 从上到下打印二叉树
class Solution {
public:
vector<int> levelOrder(TreeNode* root) {
queue<TreeNode*> queue;
vector<int> res;
if(!root) return res;
queue.push(root);
while(!queue.empty()){
for(int i=0;i<queue.size();i++){
TreeNode* temp;
temp = queue.front();
res.push_back(temp->val);
queue.pop();
if(temp->left) queue.push(temp->left);
if(temp->right) queue.push(temp->right);
}
}
return res;
}
};
32 - II. 从上到下打印二叉树 II
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>>res;
if(root==NULL)return res;
queue<TreeNode*>q;
q.push(root);
while(!q.empty()){
vector<int>r;
int l=q.size();
for(int i=0;i<l;i++){
TreeNode* t=q.front();
r.push_back(t->val);
q.pop();
if(t->left)q.push(t->left);
if(t->right)q.push(t->right);
}
res.push_back(r);
}
return res;
}
};
32 - III. 从上到下打印二叉树 III
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>>res;
if(root==NULL)return res;
queue<TreeNode*>q;
q.push(root);
bool flag = 1;
while(!q.empty()){
vector<int>r;
int l=q.size();
for(int i=0;i<l;i++){
TreeNode* t=q.front();
r.push_back(t->val);
q.pop();
if(t->left)q.push(t->left);
if(t->right)q.push(t->right);
}
if(!flag) res.push_back(vector<int>(r.rbegin(),r.rend()));
else res.push_back(r);
flag = !flag;
}
return res;
}
};
Offer 33. 二叉搜索树的后序遍历序列
class Solution {
public:
bool verifyPostorder(vector<int>& postorder) {
if(postorder.size()<2) return true;
return dfs(postorder,0,postorder.size()-1);
}
bool dfs(vector<int>& postorder,int left,int right){
if(left>=right) return true;
int root = postorder[right];
int k =left;
while(postorder[k]<root) k++;
for(int i=k;i<right;i++) if(postorder[i]<root) return false;
return dfs(postorder,left,k-1)&&dfs(postorder,k,right-1);
}
};
34. 二叉树中和为某一值的路径
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> ret;
vector<int> temp;
dfs(temp, root, sum, ret);
return ret;
}
void dfs(vector<int>& cur, TreeNode* node, int target, vector<vector<int>>& ret)
{
if(!node)return;
cur.push_back(node->val);
target -= node->val;
if(!target && !node->left && !node->right)ret.push_back(cur);
dfs(cur, node->left, target, ret);
dfs(cur, node->right, target, ret);
cur.pop_back();
}
};
35. 复杂链表的复制
class Solution {
public:
Node* copyRandomList(Node* head) {
if(!head) return nullptr;
Node* p = head;
unordered_map<Node*,Node*> map;
while(p){
map[p] = new Node(p->val);
p = p->next;
}
p = head;
while(p){
map[p]->next = p->next?map[p->next]:NULL;
map[p]->random = p->random?map[p->random]:NULL;
p = p->next;
}
return map[head];
}
};
36. 二叉搜索树与双向链表
class Solution {
public:
vector<Node*> temd;
void dfs(Node* root){
if(!root) return;
dfs(root->left);
temd.push_back(root);
dfs(root->right);
}
Node* treeToDoublyList(Node* root) {
if(!root) return nullptr;
dfs(root);
for(int i=0;i<temd.size()-1;i++){
temd[i]->right = temd[i+1];
temd[i+1]->left = temd[i];
}
temd[0]->left = temd[temd.size()-1];
temd[temd.size()-1]->right = temd[0];
return temd[0];
}
};
37. 序列化二叉树 丢你个嗨写不出来,没思路~
在这里插入代码片
38. 字符串的排列
class Solution {
public:
vector<string> permutation(string s) {
vector<string> res;
sort(s.begin(),s.end());
dfs(res,"",s);
return res;
}
void dfs(vector<string> &res,string temp,string s){
if(temp.size() == s.size()) {res.push_back(temp);return;}
for(int i=0;i<s.size();++i){
if(s[i]=='*'||(i>0&&s[i]==s[i-1])) continue;
char a = s[i];
temp = temp+s[i];
s[i] = '*';
dfs(res,temp,s);
s[i] = a;
temp.pop_back();
}
}
};