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;
}