1.栈部分
#include<iostream>
using namespace std;
struct listnode
{
int val;
listnode* next;
listnode(int x) :val(x), next(nullptr) {}
};
listnode* listcreate() //创建
{
listnode * phead = new listnode(0);
listnode * pcurrent = nullptr;
pcurrent = phead;
int data;
while (cin>>data)
{
listnode* pnew = new listnode(data);
pcurrent->next = pnew;
pcurrent = pnew;
}
return phead->next;
}
void printlist(listnode *phead) //打印
{
listnode * temp = nullptr;
temp = phead;
while (temp)
{
cout << temp->val;
temp = temp->next;
}
delete temp;
}
listnode* insertnode(listnode* phead,int x, int y) //插入y到x之后
{
listnode * pcurrent = nullptr;
pcurrent = phead;
while (pcurrent->val!=x)
{
pcurrent = pcurrent->next;
}
listnode *pnew = new listnode(y);
pnew->next = pcurrent->next;
pcurrent->next = pnew;
return phead;
}
listnode *deleteData(listnode* phead,int data) //删除节点为data的节点
{
listnode* pcurrent = nullptr;
listnode* prev = nullptr;
pcurrent = phead;
prev = phead;
while (pcurrent->val!=data&&pcurrent!=nullptr)
{
prev = pcurrent;
pcurrent = pcurrent->next;
if (pcurrent == nullptr)
{
cout << " 无";
return phead;
}
}
prev->next = pcurrent->next;
delete pcurrent;
// delete prev; 这个prev不能删除= =
return phead;
}
int destroylist(listnode *phead)//销毁链表
{
listnode *ptem = nullptr;
ptem = phead;
while (phead)
{
ptem = ptem->next;
delete phead;
phead = ptem;
}
return 0;
}
listnode* reverselist(listnode*phead)//翻转链表
{
listnode* prev = nullptr;
listnode* pcurrent = nullptr;
listnode* pnext = nullptr;
prev = phead;
pcurrent = prev->next;
while (pcurrent)
{
pnext = pcurrent->next;
pcurrent->next = prev;
prev = pcurrent;
pcurrent = pnext;
}
phead->next = nullptr;
return prev;
}
listnode* bottomKnode(listnode* phead,int k)//倒数第K个节点
{ /*
listnode* p1 = phead;
listnode* p2 = phead;
int count = 0;
while (p1)
{
p1 = p1->next;
count++;
}
for (int i =1; i <=count-k; i++)
{
p2 = p2->next;
}
*/
listnode* p1 = phead;
listnode* p2 = phead;
for (int i = 0; i < k-1; i++)
{
p1 = p1->next;
}
while (p1->next!=nullptr)
{
p1 = p1->next;
p2 = p2->next;
}
return p2;
}
listnode* merge(listnode*phead1, listnode*phead2)
{
if (phead1==nullptr)
{
return phead2;
}
else
{
if (phead2==nullptr)
{
return phead1;
}
}
listnode *mergenode=nullptr;
if (phead1->val>phead2->val)
{
mergenode = phead1;
mergenode->next = merge(phead1->next, phead2);
}
else
{
mergenode = phead2;
mergenode->next = merge(phead1, phead2->next);
}
return mergenode;
}
listnode* commonnode(listnode*phead1, listnode*phead2)//找出公共的第一个节点
{
int len1 = getlistlen(phead1);
int len2 = getlistlen(phead2);
listnode*shortlist = nullptr;
listnode*longlist = nullptr;
listnode*commonnode = nullptr;
int step = 0;
if (len1>len2)
{
step = len1 - len2;
longlist = phead1;
shortlist = phead2;
}
else
{
step = len2 - len1;
longlist = phead2;
shortlist = phead1;
}
for (int i = 0; i < step; i++)
{
longlist = longlist->next;
}
while (longlist!=nullptr&&shortlist!=nullptr)
{
longlist = longlist->next;
shortlist = shortlist->next;
if (longlist == shortlist)
{
commonnode = longlist;
break;
}
}
return commonnode;
}
int getlistlen(listnode *phead)
{
listnode *temp = phead;
int len = 0;
while (temp)
{
temp = temp->next;
len++;
}
return len;
}
int main()
{
//新建链表测试
listnode* node = listcreate();
//打印链表测试
printlist(node);
cout << endl;
//插入链表测试
listnode* node5 = insertnode(node ,3,4);
printlist(node5);
cout << endl;
//删除链表测试
listnode* node6 = deleteData(node,6);
printlist(node6);
cout << endl;
//翻转链表测试
listnode* node7 = reverselist(node);
printlist(node7);
}
2.树部分
1.二叉树的深度
结构
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
开始用DFS,爆栈
相当简洁的解法而且有动态规划内味
class Solution {
public:
int TreeDepth(TreeNode* pRoot){
if(!pRoot) return 0 ;
return max(1+TreeDepth(pRoot->left), 1+TreeDepth(pRoot->right));
}
};
2.二叉树的镜像
这道题算是写出来但是有个问题,不能把临时指针delete?
class Solution {
public:
void Mirror(TreeNode *pRoot) {
if (pRoot == nullptr)
{ return;}
TreeNode* tmp=pRoot->left;
pRoot->left=pRoot->right;
pRoot->right=tmp;
//delete tmp;错误 删除
Mirror(pRoot->left);
Mirror(pRoot->right);
}
};