您的位置:首页 > Web前端

剑指offer(三)

2015-11-05 15:48 417 查看
1.从后面往前面打印链表

#include <iostream>
#include <string>
#include <list>
#include <stack>
using namespace std;
void revert_list_print(list<int> l1)
{
stack<int> l2;
for (list<int>::iterator ite=l1.begin();ite!=l1.end();ite++) l2.push(*ite);
while (!l2.empty())
{
cout<<l2.top();
l2.pop();
}

}
int main()
{
int a[]={1,2,3,4,5,6};
list<int>l1(a,a+6);
revert_list_print(l1);
return 0;
}
这个是用标准库中的list实现的,算法思想还在,存入栈中,从栈中输出。当然用栈也就可以用递归了,所以下面可以看下递归的代码:

#include <iostream>
#include <string>
#include <list>
#include <vector>
using namespace std;
void revert_list_print(list<int> &l1,list<int>::iterator ite)
{
if (ite!=l1.end())
{
ite++;
if((ite)!=l1.end())
revert_list_print(l1,ite);
ite--;
cout<<*ite;
}

}
int main()
{
int a[]={1,2,3,4,5,6};
list<int>l1(a,a+6);
revert_list_print(l1,l1.begin());
return 0;
}
由于平时不是很经常使用list,所以在开始时,传递list的时候,没有加引用,这个就导致出问题了。list在进行传参数时记得加引用。但是递归有时会导致递归栈很深,所以一般还是用栈好了。

时间复杂度当然是o(n),空间复杂度为o(1)。

2.利用栈的方法遍历树

前序遍历:

void qx(my_tree *root)
{
vector<my_tree*>v1;
while (root||!v1.empty())
{
while(root)
{
v1.push_back(root);
cout<<root->data;
root=root->left;
}
if(!v1.empty())
{
root=*(v1.end()-1);
v1.pop_back();
root=root->right;

}
}
}
中序遍历:

void zx(my_tree *root)
{
vector<my_tree*>v1;
while (root||!v1.empty())
{
while(root)
{
v1.push_back(root);
root=root->left;
}
if(!v1.empty())
{
root=*(v1.end()-1);
cout<<root->data;
v1.pop_back();
root=root->right;
}
}
}


后序遍历:

void hx(my_tree *root)
{
vector<my_tree*> v1;
my_tree *curr = root ;
my_tree *previsited = NULL;
while(curr != NULL || !v1.empty())
{
while(curr != NULL)
{
v1.push_back(curr);
curr = curr->left;
}
curr = *(v1.end()-1);
if(curr->right == NULL || curr->right == previsited)
{
cout<<curr->data<<"  ";
previsited = curr;
v1.pop_back();
curr = NULL;
}
else
curr = curr->right;
}
}
3.利用前序和中序遍历重建二叉树

//已知前序和中序遍历重建二叉树
#include<iostream>
#include<vector>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> in)
{
int len2=in.size();
int len1=pre.size();
if(len1==0||len2==0)
return NULL;
vector<int> preL,preR,inL,inR;
TreeNode* root=new TreeNode(pre[0]);
int index;
for(int i=0;i<len2;i++){
if(in[i]==pre[0]){
index=i;
break;
}
}
for(int i=0;i<index;i++){
preL.push_back(pre[i+1]);
inL.push_back(in[i]);
}
for(int i=index+1;i<len2;i++){
preR.push_back(pre[i]);
inR.push_back(in[i]);
}
root->left=reConstructBinaryTree(preL,inL);
root->right=reConstructBinaryTree(preR,inR);
return root;

}

int main()
{
int a[]={1,2,4,7,3,5,6,8};
int b[]={4,7,2,1,5,3,8,6};
vector<int >v1(a,a+8),v2(b,b+8);
TreeNode* tree=reConstructBinaryTree(v1,v2);
return 0;
}
4.旋转数组的最小数(二分查找,不存在重叠的数,严格单调递增)

#include <iostream>
using namespace std;
int  xuxs(int a[],int low,int high)
{
if(a[high]>a[low])
return a[low];
while(high-low>1)
{
int mid=(low+high)/2;
if(a[mid]>a[low])low=mid;
else if (a[mid]<a[high]) high=mid;
}
return a[high];
}
int main()
{
int a[]={6,7,8,9,10,1,2,3,4,5};
int len=sizeof(a)/sizeof(a[0]);
int  k=xuxs(a,0,len-1);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: