【21-25】剑指offer

21.题目描述

输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列45,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)

注意:解题目的逻辑

#include <iostream>

#include <vector>

#include <stack>

using namespace std;

 

class Solution {

public:

   bool IsPopOrder(vector<int> pushV,vector<int> popV) {

              //如果压栈序列和弹栈序列元素个数都为0,则返回false

              if(pushV.size()==0|| popV.size() == 0)

                     returnfalse;

              //如果两个栈的元素个数不相等,则返回false

              if(pushV.size()!= popV.size())

                     returnfalse;

              //当压栈和弹栈序列元素个数不为0时

              stack<int>push_st;

              vector<int>::iteratorite_pop = popV.begin();

              vector<int>::iteratorite_push = pushV.begin();

              while(ite_push!= pushV.end())

              {

                     if(push_st.empty())

                            push_st.push(*ite_push);

                     else

                     {

                            if(*ite_pop!= push_st.top())

                            {

                                   ite_push++;

                                   if(ite_push!=pushV.end())

                                          push_st.push(*ite_push);

                                   else

                                          break;

                            }

                            else

                            {

                                   cout<<push_st.top()<<endl;

                                   push_st.pop();

                                   ite_pop++;

                            }

                     }

              }

 

              if(ite_push== pushV.end() && ite_pop == popV.end())

                     returntrue;

              else

                     returnfalse;

    }

};

 

int main()

{

       freopen("data.txt","r",stdin);

       vector<int>pushV(5);

       vector<int>::iteratorite = pushV.begin();

       for(;ite != pushV.end(); ite++)

       {

              //cout<<"Pleaseinput the pushV:";

              cin>>*ite;

       }

 

       vector<int>popV(5);

       ite= popV.begin();

       for(;ite != popV.end(); ite++)

       {

              //cout<<"Pleaseinput the pushV:";

              cin>>*ite;

       }

 

       Solutions;

       if(s.IsPopOrder(pushV,popV))

              cout<<"Yse"<<endl;

       else

              cout<<"No"<<endl;

 

       return0;

}

 

22.题目描述

从上往下打印出二叉树的每个节点,同层节点从左至右打印。

树的层序遍历:

#include <iostream>

#include <vector>

#include <queue>

using namespace std;

 

struct TreeNode {

       intval;

       structTreeNode *left;

       structTreeNode *right;

       TreeNode(intx) :

                     val(x),left(NULL), right(NULL) {

       }

};

 

class Solution {

public:

   vector<int> PrintFromTopToBottom(TreeNode *root) {

              if(root== NULL)

                     returnvector<int>(0);

             

              queue<TreeNode*> que;

              queue<int>equdata;

 

              que.push(root);

              while(que.size())

              {

                     TreeNode*node = que.front();

                     que.pop();

 

                     cout<<node->val<<"";

                     equdata.push(node->val);

 

                     if(node->left)

                            que.push(node->left);

                     if(node->right)

                            que.push(node->right);

              }

 

              vector<int>array(equdata.size());

              vector<int>::iteratorite = array.begin();

              while(!equdata.empty())

              {

                     *ite= equdata.front();

                     equdata.pop();

                     ite++;

              }

 

              returnarray;

    }

       //创建二叉树

       TreeNode* CreateTree()

       {

              intval = 0;

              cout<<"Pleaseinput val(-1):";

              cin>>val;

              if(val== -1)

                     returnNULL;

              TreeNode*root = new TreeNode(val);

              root->left=CreateTree();

              root->right=CreateTree();

              returnroot;

       }

       //前序打印二叉树

       voidPrePrintTree(TreeNode *root)

       {

              if(root== NULL)

                     return;

              cout<<root->val<<"";

              PrePrintTree(root->left);

              PrePrintTree(root->right);

       }

};

 

int main()

{

       //freopen("data.txt","r",stdin);

       Solutions;

       TreeNode* root = s.CreateTree();

       s.PrePrintTree(root);

       cout<<endl;

       vector<int>temp = s.PrintFromTopToBottom(root);

       for(vector<int>::iteratorite = temp.begin(); ite != temp.end(); ite++)

       {

              cout<<*ite<<"";

       }

       return0;

}

 

23.题目描述

输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。

注意:二叉搜索树的根节点大于左子节点的值,小于右子节点的值。

每一次取出序列的最后一个值,遍历序列(除最后一个结点外)

如果发现结点的数大于根节点的值,怎停止循环,记录从该序列的开始到停止的地方为,左子树部分;然后接着遍历剩下的序列至根节点的前一个结点,如果发现结点的值小于根节点的值,则返回false。否则:如果左子树在序列中的最后一个结点在序列中的位置大于0,则递归左子树;若右子树结点个数小于总数目-左子树的数目-1,则遍历右子树。

 

#include<iostream>

#include<vector>

using namespace std;

 

class Solution {

public:

   bool VerifySquenceOfBST(vector<int> sequence) {

              //如果输入序列为空,则返回false

              if(sequence.size()== 0)

                     returnfalse;

              //得到序列最后一个结点的值,即根节点的值

              vector<int>::iteratorite = sequence.end() - 1;

              //在序列中搜索小于根节点的左子节点

              vector<int>::iteratorLite = sequence.begin();

              for(;Lite != sequence.end()-1; Lite++)

              {

                     if(*Lite> *ite)

                            break;

              }

              //在序列中搜索大于根节点的右子节点

              vector<int>::iteratorRite = Lite;

              for(;Rite != sequence.end()-1; Rite++)

              {

                     if(*Rite< *ite)

                            returnfalse;

              }

              //遍历左右不分判断段是够符合

              boolleft = true;

              if(Lite> sequence.begin())

                     left= VerifySquenceOfBST(vector<int>(sequence.begin(), Lite));

              boolright = true;

              if(Lite< sequence.end()-1)

                     right= VerifySquenceOfBST(vector<int>(Lite, sequence.end()-1));

 

              if(left&& right)

                     returntrue;

              else

                     returnfalse;

    }

};

 

int main()

{

       freopen("data.txt","r", stdin);

       vector<int>sequence(7);

       vector<int>::iteratorite = sequence.begin();

       for(;ite!= sequence.end(); ite++)

              cin>>*ite;

 

       Solutions;

       if(s.VerifySquenceOfBST(sequence))

              cout<<"Yes"<<endl;

       else

              cout<<"No"<<endl;

 

       return0;

}


24.题目描述

输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。

    注意:递归思想,

#include<iostream>

#include<vector>

using namespace std;

 

struct TreeNode {

       intval;

       structTreeNode *left;

       structTreeNode *right;

       TreeNode(intx) :

                     val(x),left(NULL), right(NULL) {

       }

};

 

class Solution {

public:

   vector<vector<int> > FindPath(TreeNode* root,intexpectNumber) {

              //如果树为NULL,则返回NULL;

              if(root== NULL)

                     returnvector<vector<int> >();

              //创建存储路劲的容器

              vector<int>st_path;

              //保存路径值

              intsumCurrent = 0;

              vector<vector<int>> Path;

              FindPath(root,sumCurrent, st_path, expectNumber, Path);

              returnPath;

    }

 

       voidFindPath(TreeNode * root, int sumCurrent, vector<int> &st_path, intexpectNumber, vector<vector<int> > &path)

       {

              //计算sumCurrent的值

              sumCurrent+= root->val;

              st_path.push_back(root->val);

 

              if(root->left== NULL && root->right == NULL && sumCurrent ==expectNumber)

              {

                     cout<<"apath is found:"<<endl;

                     path.push_back(st_path);

                     vector<int>::iteratorit = st_path.begin();

                     for(;it!= st_path.end(); it++)

                     {

                            cout<<*it<<"";

                     }

                     cout<<endl;

              }

             

              if(root->left!= NULL)

                     FindPath(root->left,sumCurrent, st_path, expectNumber, path);

              if(root->right!= NULL)

                     FindPath(root->right,sumCurrent, st_path, expectNumber, path);

             

              st_path.pop_back();

       }

      

       //创建二叉树

       TreeNode* CreateTree()

       {

              intval = 0;

              cout<<"Pleaseinput val(-1):";

              cin>>val;

              if(val== -1)

                     returnNULL;

              TreeNode*root = new TreeNode(val);

              root->left=CreateTree();

              root->right=CreateTree();

              returnroot;

       }

       //前序打印二叉树

       voidPrePrintTree(TreeNode *root)

       {

              if(root== NULL)

                     return;

              cout<<root->val<<"";

              PrePrintTree(root->left);

              PrePrintTree(root->right);

       }

};

 

int main()

{

       Solutions;

       TreeNode*root = s.CreateTree();

       s.PrePrintTree(root);

       cout<<endl;

       vector<vector<int>> temp = s.FindPath(root, 22);

       cout<<endl;

       vector<vector<int>>::iterator ite1 = temp.begin();

       vector<int>::iteratorite2 = ite1->begin();

       for(;ite1!= temp.end(); ite1++)

              for(ite2= ite1->begin(); ite2 != ite1->end(); ite2++)

                     cout<<*ite2<<"";

       cout<<endl;

       return0;

}

 

25.题目描述

输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)

分为三步进行:1. 复制N,创建N’ 然后将其连接起来

              2. 修改N’ 的随机指针域指向

              3. 按照奇数和偶数结点的方式拆分链表

 

/*

struct RandomListNode {

   int label;

   struct RandomListNode *next, *random;

   RandomListNode(int x) :

           label(x), next(NULL), random(NULL) {

    }

};

*/

class Solution {

public:

   RandomListNode* Clone(RandomListNode* pHead)

    {

              //复制链表由N-->N'

              CloneNodes(pHead);

              //连接随机指针域

              ConnectionSiblingNodes(pHead);

              //拆分链表

              returnReconnectionNodes(pHead);

    }

      

       voidCloneNodes(RandomListNode* pHead)

       {

              //如果链表为NULL,则返回

              if(pHead== NULL)

                     return;

             

              RandomListNode*pNode = pHead;

              while(pNode!= NULL)

              {

                     RandomListNode*node = new RandomListNode(0);

                     node->label= pNode->label;

                     node->next= pNode->next;

                     node->random= NULL;

                    

                     pNode->next= node;

 

                     pNode= node->next;

              }

       }

      

       voidConnectionSiblingNodes(RandomListNode* pHead)

       {

              if(pHead== NULL)

                     return;

             

              RandomListNode*pNode = pHead;

              while(pNode!= NULL)

              {

           RandomListNode* pClone = pNode->next;

                     if(pNode->random!= NULL)

                            pClone->random= pNode->random->next;

                     pNode= pClone->next;

              }

             

       }

 

       RandomListNode*ReconnectionNodes(RandomListNode* pHead)

       {

              if(pHead== NULL)

                     returnNULL;

 

              RandomListNode*pNode = pHead;

              RandomListNode*pClone = NULL;

              RandomListNode*pCloneHead = NULL;

 

              if(pNode!= NULL)

              {

                     pCloneHead= pClone = pNode->next;

                     pNode->next= pClone->next;

                     pNode= pNode->next;

              }

 

              while(pNode!= NULL)

              {

                     pClone->next= pNode->next;

                     pClone= pClone->next;

                     pNode->next= pClone->next;

                     pNode= pNode->next;

              }

 

              returnpCloneHead;

       }

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值