1合并多个有序链表
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
struct ListNode
{
int val;
ListNode *next;
};
//用二元谓词定义类似于比较器的东西
struct ListNodeComparator{
bool operator()(ListNode *a,ListNode *b)
{
return a->val>b->val;
}
};
ListNode* mergeKLists(vector<ListNode*>& lists)
{
if(lists.empty())
return NULL;
priority_queue<ListNode*,vector<ListNode*>,ListNodeComparator> heap;
for(int i=0;i<lists.size();i++)
{
if(lists[i]!=NULL)
heap.push(lists[i]);
}
if(heap.empty())
return NULL;
//先取出头节点
ListNode *head=heap.top();
heap.pop();
ListNode *pre=head;
if(pre->next)//将pre节点后面的进小根堆
{
heap.push(pre->next);
}
while(!heap.empty())//一直到堆为空
{
ListNode *cur=heap.top();
heap.pop();
pre->next=cur;
pre=cur;
if(cur->next!=NULL)
heap.push(cur->next);
}
return head;
}
2 判断两颗树是否结构相同
#include<iostream>
using namespace std;
class TreeNode{
public:
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int val):val(val),left(NULL),right(NULL){
}
};
bool isSameTree(TreeNode* p, TreeNode* q)//判断两棵树相等的递归条件:该节点值相等且
//左右子树相等 ,递归结束条件:有一个为空或者全为空
{
if(p==NULL&&q==NULL)
return 1;
if((p==NULL)^(q==NULL))
return 0;
return p->val==q->val&&isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
}
3判断一棵树是否是镜面树
#include<iostream>
using namespace std;
class TreeNode{
public:
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int val):val(val),left(NULL),right(NULL){
}
};
bool isSymmetric(TreeNode* root) {
return isMirror(root, root);
}
bool isMirror(TreeNode* h1, TreeNode* h2)//判断两棵树是否对称,即两节点的值相等
//你的左树等于我的右树,你的右树等于我的左树
{
if((h1==NULL)^(h2==NULL))
return false;
if(h1==NULL&&h2==NULL)
return true;
return h1->val==h2->val&&isMirror(h1->left,h2->right)&&isMirror(h1->right,h2->left);
}
4返回一棵树的最大深度
#include<iostream>
using namespace std;
class TreeNode{
public:
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int val):val(val),left(NULL),right(NULL){
}
};
int maxDepth(TreeNode* root)//最大深度为左子树和右子树最大深度加1
{
if (root == nullptr) {
return 0;
}
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
5用先序数组和中序数组重建一棵树
#include <iostream>
#include <vector>
using namespace std;
class TreeNode {
public:
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int val) : val(val), left(nullptr), right(nullptr) {}
};
TreeNode* buildTree1(vector<int>& preorder, vector<int>& inorder)
{
if(preorder.empty()||inorder.empty()||preorder.size()!=inorder.size())
return NULL;
return f(preorder,0,preorder.size()-1,inorder,0,preorder.size()-1);
}
TreeNode* f(vector<int>&preorder,int l1,int r1,vector<int>&inorder,int l2,int r2)
{
if(l1>r1)
return NULL;
TreeNode *head=new TreeNode(preorder[l1]);
if(l1==r1)
return head;
int find=l2;
while(inorder[find]!=preorder[l1])
{
find++;
}
head->left=f(preorder,l1+1,l1+find-l2,inorder,l2,find-1);
head->right=f(preorder,l1+find-l2+1,r1,inorder,find+1,r2);
return head;
}
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
class TreeNode {
public:
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int val) : val(val), left(nullptr), right(nullptr) {}
};
//用哈希表优化查找过程
TreeNode *buildTree(vector<int>&preorder,vector<int>&inorder)
{
if(preorder.empty()||inorder.empty()||preorder.size()!=inorder.size())
return NULL;
unordered_map<int ,int> index;
for(int i=0;i<inorder.size();i++)
{
index[inorder[i]]=i;
}
return g(preorder,0,preorder.size()-1,inorder,0,inorder.size()-1,index);
}
TreeNode * g(vector<int>& preorder, int l1, int r1, vector<int>& inorder, int l2, int r2,
unordered_map<int, int>& value)
{
if(l1>r1)
return NULL;
TreeNode *head=new TreeNode(preorder[l1]);
if(l1==r1)
return head;
int find=value[preorder[l1]];
head->left=g(preorder,l1+1,l1+find-l2,inorder,l2,find-1,value);
head->right=g(preorder,l1+find-l2+1,r1,inorder,find+1,r2,value);
return head;
}
6比较器使用
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Student {
public:
string name;
int id;
int age;
Student(string name, int id, int age) : name(name), id(id), age(age) {}
};
class IdComparator {
public:
int operator()(const Student& o1, const Student& o2) const {
if (o1.id < o2.id) {
return true;
}
else if (o2.id < o1.id) {
return false;
}
else {
return false;
}
}
};
class AgeComparator {
public:
int operator()(const Student& o1, const Student& o2) const {
if (o1.age < o2.age) {
return true;
}
else if (o2.age < o1.age) {
return false;
}
else {
return false;
}
}
};
void printArray(vector<int>& arr) {
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
cout << endl;
}
void printStudents(vector<Student>& students) {
for (int i = 0; i < students.size(); i++) {
cout << students[i].name << ", " << students[i].id << ", " << students[i].age << endl;
}
}
int main() {
vector<int> arr = { 8, 1, 4, 1, 6, 8, 4, 1, 5, 8, 2, 3, 0 };
printArray(arr);
sort(arr.begin(), arr.end());
printArray(arr);
Student s1("张三", 5, 27);
Student s2("李四", 1, 17);
Student s3("王五", 4, 29);
Student s4("赵六", 3, 9);
Student s5("左七", 2, 34);
vector<Student> students = { s1, s2, s3, s4, s5 };
printStudents(students);
cout << "=======" << endl;
sort(students.begin(), students.end(), IdComparator());
printStudents(students);
cout << "=======" << endl;
vector<Student> arrList;
arrList.push_back(s1);
arrList.push_back(s2);
arrList.push_back(s3);
arrList.push_back(s4);
arrList.push_back(s5);
for (const Student& s : arrList) {
cout << s.name << ", " << s.id << ", " << s.age << endl;
}
cout << "=======" << endl;
sort(arrList.begin(), arrList.end(), AgeComparator());
for (const Student& s : arrList) {
cout << s.name << ", " << s.id << ", " << s.age << endl;
}
return 0;
}
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#include <functional> // for std::greater
using namespace std;
class Student {
public:
string name;
int id;
int age;
Student(string name, int id, int age) : name(name), id(id), age(age) {}
};
// 定义 MyComparator 类
class MyComparator {
public:
int operator()(int o1, int o2) const {
if (o1 < o2) {
return 1;
} else if (o1 > o2) {
return -1;
} else {
return 0;
}
}
};
// 定义 IdComparator 类
class IdComparator {
public:
bool operator()(const Student& o1, const Student& o2) const {
return o1.id < o2.id; // 按 id 的升序排列
}
};
int main() {
priority_queue<int, vector<int>, MyComparator> heap; // 使用 MyComparator 对整数进行比较
string str1 = "abc";
string str2 = "b";
cout << str1.compare(str2) << endl;
priority_queue<Student, vector<Student>, IdComparator> studentHeap; // 使用 IdComparator 对 Student 对象进行比较
Student s1("张三", 5, 27);
Student s2("李四", 1, 17);
Student s3("王五", 4, 29);
Student s4("赵六", 3, 9);
Student s5("左七", 2, 34);
studentHeap.push(s1);
studentHeap.push(s2);
studentHeap.push(s3);
studentHeap.push(s4);
studentHeap.push(s5);
cout << "=========" << endl;
while (!studentHeap.empty()) {
Student s = studentHeap.top();
studentHeap.pop();
cout << s.name << ", " << s.id << ", " << s.age << endl;
}
return 0;
}
7二叉树先序、中序、后序遍历
#include <iostream>
using namespace std;
class Node {
public:
int value;
Node* left;
Node* right;
Node(int v) : value(v), left(nullptr), right(nullptr) {}
};
void f(Node* head) {
if (head == nullptr) {
return;
}
// 1
f(head->left);
// 2
f(head->right);
// 3
}
// 先序打印所有节点
void pre(Node* head) {
if (head == nullptr) {
return;
}
cout << head->value << endl;
pre(head->left);
pre(head->right);
}
void in(Node* head) {
if (head == nullptr) {
return;
}
in(head->left);
cout << head->value << endl;
in(head->right);
}
void pos(Node* head) {
if (head == nullptr) {
return;
}
pos(head->left);
pos(head->right);
cout << head->value << endl;
}