劍指offer(六)

26. 順時針打印矩陣

因爲弄反了行列,所以因爲溢出折騰了好一會。做之前感覺遞歸會更方便,現在還是覺得循環更簡單些。

class Solution {
public:
    void printM(vector<vector<int> > matrix,int rowbegin,int rowend,int colbegin,int colend,vector<int>& result){
        if(rowbegin>rowend || colbegin>colend) 
            return;
        int i=colbegin,j=rowbegin;
        if(rowbegin==rowend){
            while(i<=colend) {result.push_back(matrix[i][j]);i++;}
            return;
        }
        else if(colbegin==colend){
            while(j<=rowend) {result.push_back(matrix[i][j]);j++;}
            return;
        }
        while(j<rowend) {result.push_back(matrix[i][j]);j++;}
        while(i<colend) {result.push_back(matrix[i][j]);i++;}
        while(j>rowbegin) {result.push_back(matrix[i][j]);j--;}
        while(i>colbegin) {result.push_back(matrix[i][j]);i--;}
        
        if(rowbegin<rowend && colbegin<colend) 
            printM(matrix,rowbegin+1,rowend-1,colbegin+1,colend-1,result);
        return;
    }
    vector<int> printMatrix(vector<vector<int> > matrix) {
        vector<int> result; 
        if(matrix.size()==0) return result;
        if(matrix[0].size()==0) return result;
        int rowend=matrix[0].size()-1;
        int colend=matrix.size()-1;
        int rowbegin=0,colbegin=0;      
        if(rowbegin<=rowend && colbegin<=colend) 
            printM(matrix,rowbegin,rowend,colbegin,colend,result);
        return result;
    }
};

27. 包含min函數的棧

class Solution {
public:
    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();
    }
};

28. 棧的壓入、彈出序列

建立輔助棧,模擬棧的壓入彈出過程看是否能全彈出.

class Solution {
public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
    	if(pushV.size()==0 || popV.size()==0) return false;
        stack<int> stk;
        int i=0;
        for(int j=0;j<pushV.size() && i<popV.size();j++){
            stk.push(pushV[j]);
            while(i<popV.size() && !stk.empty() && stk.top()==popV[i]){
                stk.pop();
                i++;
            }
        }
        if(stk.empty()) return true;
        return false;
    }
};

29. 打印二叉樹

29.1 從上到下打印二叉樹

使用隊列,先進先出,按順序保留每個子節點。

class Solution {
public:
    vector<int> PrintFromTopToBottom(TreeNode* root) {
        vector<int> print;
        if(root==NULL) return print;
        queue<TreeNode*> que;
        que.push(root);
        while(!que.empty()){
            root=que.front();            
            que.pop();
            if(root==NULL) continue;
            print.push_back(root->val);
            que.push(root->left);
            que.push(root->right);
        }
        return print;
    }
};

29.2. 分行從上到下打印二叉樹

使用一個int值計數,記下一層節點個數countnext,當前節點個數count會在遍歷完當前層之後重新賦值爲countnext,開始遍歷下一層。

class Solution {
public:
        vector<vector<int> > Print(TreeNode* pRoot) {
            vector<vector<int>> result;
            vector<int> col;
            if(pRoot==NULL) return result;
            queue<TreeNode*> que;
            que.push(pRoot);
            int count=1,countnext=0;
            while(!que.empty()){
                pRoot=que.front();
                if(pRoot->left!=NULL){
                    que.push(pRoot->left);
                    countnext++;
                } 
                if(pRoot->right!=NULL){
                    que.push(pRoot->right);
                    countnext++;
                }
                que.pop();
                col.push_back(pRoot->val);
                count--;
                if(count==0){
                    result.push_back(col);
                    col.clear();
                    count=countnext;
                    countnext=0;
                }
            }            
            return result;
        }
};

29.3 之字形打印二叉樹

class Solution {
public:
        vector<vector<int> > Print(TreeNode* pRoot) {
            vector<vector<int>> result;
            vector<int> col;
            if(pRoot==NULL) return result;
            stack<TreeNode*> stknext;
            stack<TreeNode*> stk;
            stk.push(pRoot);
            bool ceng=true;//分奇偶層;
            int count=1,countnext=0;
            while(!stk.empty()){
                pRoot=stk.top();
                //收集下一層的節點
                if(pRoot->left!=NULL && ceng){
                    stknext.push(pRoot->left);
                    countnext++;
                } 
                if(pRoot->right!=NULL){
                    stknext.push(pRoot->right);
                    countnext++;
                }
                if(pRoot->left!=NULL && !ceng){
                    stknext.push(pRoot->left);
                    countnext++;
                } 
                stk.pop();
                col.push_back(pRoot->val);
                count--;
                if(count==0){
                    //捋一下下一層節點的順序
                    result.push_back(col);
                    col.clear();
                    count=countnext;
                    countnext=0;
                    ceng=!ceng;
                    stk=stknext;
                    //因爲沒有能直接清空棧的方法,用了一個笨方法。
                    stack<TreeNode*> empt;
                    stknext=empt;
                }
            }            
            return result;
        }
};

30. 二叉搜索樹的後序遍歷序列

class Solution {
public:
    bool VerifySquenceOfBST(vector<int> sequence) {
        //搜索樹默認中序遍歷有序,所以相當於找中序與後序的聯繫
        int size=sequence.size();
        if(size==0) return false;
        if(size==1) return true;
        vector<int> left;
        vector<int> right;
        int i=0;     
        bool leftflag=true;
        while(i<size-1){
        	//左子樹
            if(sequence[i]<sequence[size-1] && leftflag){
                left.push_back(sequence[i]);
                i++;                
            }
            //右子樹
            else if(sequence[i]>sequence[size-1]){
                leftflag=false;
                right.push_back(sequence[i]);
                i++;               
            }
            //應該在右子樹的但小於根節點,異常
            else return false;
        }
        //最初輸入的樹爲空應 false;這裏爲了區別最初的空還是後來的空。
        bool leftBST=left.size()<=1? true :VerifySquenceOfBST(left);
        bool rightBST=right.size()<=1? true :VerifySquenceOfBST(right);
        return  leftBST && rightBST;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章