【剑指offer】(29~32)

本文提供了一系列经典算法题目的解决方案,包括判断二叉树是否对称、顺时针打印矩阵、实现带有min函数的栈、验证栈的压入弹出序列以及多种方式打印二叉树的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

28:对称的二叉树

请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。

    bool isSymmetrical(TreeNode* pRoot)
    {
        if(pRoot==NULL)
            return true;
        return comRoot(pRoot->left,pRoot->right);
    }
    bool comRoot(TreeNode *left,TreeNode *right)
    {
        if(left==NULL) return right==NULL;
        if(right==NULL) return false;
        if(left->val!=right->val) return false;
        return comRoot(left->right,right->left)&&comRoot(left->left,right->right);
    }

29:顺时针打印矩阵

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。

    vector<int> printMatrix(vector<vector<int> > matrix) 
    {
        int row = matrix.size();
        int col = matrix[0].size();
        vector<int> res;

        // 输入的二维数组非法,返回空的数组
        if (row == 0 || col == 0)  return res;

        // 定义四个关键变量,表示左上和右下的打印范围
        int left = 0, top = 0, right = col - 1, bottom = row - 1;
        while (left <= right && top <= bottom)
        {
            // left to right
            for (int i = left; i <= right; ++i)  res.push_back(matrix[top][i]);
            // top to bottom
            for (int i = top + 1; i <= bottom; ++i)  res.push_back(matrix[i][right]);
            // right to left
            if (top != bottom)
            for (int i = right - 1; i >= left; --i)  res.push_back(matrix[bottom][i]);
            // bottom to top
            if (left != right)
            for (int i = bottom - 1; i > top; --i)  res.push_back(matrix[i][left]);
            left++,top++,right--,bottom--;
        }
        return res;
    }

30:包含min函数的栈

定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。

    //注意时间复杂度
    stack<int> stack1,stack2;
     
    void push(int value) {
        stack1.push(value);
        if(stack2.empty())
            stack2.push(value);
        else if(value<=stack2.top())
        {
            stack2.push(value);
        }
    }
     
    void pop() {
        if(stack1.top()==stack2.top())
            stack2.pop();
        stack1.pop();
         
    }
     
    int top() {
        return stack1.top();       
    }
     
    int min() {
        return stack2.top();
    }

31:栈的压入、弹出序列

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

 bool IsPopOrder(vector<int> pushV,vector<int> popV) {
        int n=pushV.size();
        int i=0,j=0;
        stack<int> s; 
        for(i;i<n;i++)
        {
            s.push(pushV[i]);
            while(j<n&&s.top()==popV[j])
            {
                s.pop();
                j++;
            }
        }
        return s.empty();
    }

32:打印二叉树

题目一(从上到下打印二叉树):从上往下打印出二叉树的每个节点,同层节点从左至右打印。

vector<int> PrintFromTopToBottom(TreeNode* root) {
        vector<int> vec;
        if(root==nullptr) 
            return vec;
        queue<TreeNode*> que;
        que.push(root);
        while(!que.empty())
        {
            TreeNode* tmp=que.front();
            vec.push_back(tmp->val);
            if(tmp->left!=nullptr) que.push(tmp->left);
            if(tmp->right!=nullptr) que.push(tmp->right);
            que.pop();
        }
        return vec;

    }

题目二(按层打印二叉树):从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。

vector<vector<int> > Print(TreeNode* pRoot) {
        vector<vector<int> > res;
        if(pRoot==NULL) return res;
        queue<TreeNode*> que;        
        que.push(pRoot);
        while(!que.empty())
        {    
            vector<int> vec;
            int s=que.size();
            for(int i=0;i<s;i++)
            {
                TreeNode* tmp=que.front();
                vec.push_back(tmp->val);
                que.pop();                         
                if(tmp->left!=NULL)
                    que.push(tmp->left);
                if(tmp->right!=NULL)
                    que.push(tmp->right);
            }
            res.push_back(vec);
        }
}

题目三(之字形打印)

vector<vector<int> > Print(TreeNode* pRoot) {
        vector<vector<int> > res;
        if(pRoot==NULL) return res;
        queue<TreeNode*> que;        
        que.push(pRoot);
        bool flag=0;
        while(!que.empty())
        {    
            vector<int> vec;////
            int s=que.size();
            for(int i=0;i<s;i++)
            {
                TreeNode* tmp=que.front();
                vec.push_back(tmp->val);
                que.pop();                         
                if(tmp->left!=NULL)
                    que.push(tmp->left);
                if(tmp->right!=NULL)
                    que.push(tmp->right);
            }
            if(flag)
                reverse(vec.begin(),vec.end());
            res.push_back(vec);
            flag=!flag;

        }
        return res;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值