剑指offer(21-25)编程题
21.输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
class Solution { public: bool IsPopOrder(vector<int> pushV,vector<int> popV) { if(pushV.empty()) return true; //空时,为真 int n = pushV.size(); stack<int> stack; int k = 0; //指向popv for(int i=0;i<n;i++){ if(pushV[i] == popV[k]){ k++; }else{ stack.push(pushV[i]); } } while(!stack.empty()){ if(stack.top() == popV[k]){ stack.pop(); k++; }else{ break; } } if(stack.empty()) return true; else return false; } };
22.从上往下打印出二叉树的每个节点,同层节点从左至右打印。
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } };*/ class Solution { public: vector<int> PrintFromTopToBottom(TreeNode* root) { vector<int> res; if (root == nullptr) return res; queue<TreeNode*> q1; queue<TreeNode*> q2; q1.push(root); while (!q1.empty()) { while (!q1.empty()) { TreeNode* p = q1.front(); if (p->left) q2.push(p->left); if (p->right) q2.push(p->right); res.push_back(p->val); q1.pop(); } swap(q1,q2); } return res; } };
23.输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
class Solution { private: bool doVerifySequenceOfBST(vector<int>& sequence) { int n = sequence.size(); if(sequence.empty()) return true; vector<int> left; vector<int> right; int rootVal = sequence[n-1]; for(int i=0;i<n-1;i++){ if(sequence[i] <= rootVal){ left.push_back(sequence[i]); }else{ break; } } int leftSize = left.size(); for(int i=leftSize;i<n-1;i++){ if(sequence[i]>=rootVal){ right.push_back(sequence[i]); }else{ break; } } if(leftSize + right.size() != sequence.size() - 1) return false; return doVerifySequenceOfBST(left) && doVerifySequenceOfBST(right); } public: bool VerifySquenceOfBST(vector<int> sequence) { int n = sequence.size(); if(sequence.empty()) return false; return doVerifySequenceOfBST(sequence); } };
24.输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
class Solution { private: void doFindPath(TreeNode* root, int expectNumber, int sum, vector<int> path, vector<vector<int>>& res) { if (root == nullptr) return; path.push_back(root->val); sum = sum + root->val; if(sum == expectNumber && !root->left && !root->right){ res.push_back(path); return; } if(sum > expectNumber) return; doFindPath(root->left, expectNumber, sum, path, res); doFindPath(root->right, expectNumber, sum, path, res); } public: vector<vector<int>> FindPath(TreeNode* root, int expectNumber) { vector<vector<int>> res; vector<int> path; doFindPath(root, expectNumber, 0, path, res); return res; } };
25.输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
class Solution { public: RandomListNode* Clone(RandomListNode* pHead) { if (pHead == nullptr) return nullptr; //copy on the next RandomListNode* p = pHead; while (p) { RandomListNode* q = new RandomListNode(p->label); q->next = p->next; q->random = nullptr; p->next = q; p = q->next; } //deal random pointer p = pHead; while (p) { if (p->random) { p->next->random = p->random->next; } p = p->next->next; } //split two list p = pHead; RandomListNode *head = p->next; RandomListNode *q = head; while (p) { p->next = q->next; p = p->next; if (p) { q->next = p->next; q = q->next; } } return head; } };