16.代码的鲁棒性:合并两个排序的链表
题目描述:
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
解法思路:
(1)判空:两链表均为空,返回空;
一链表为空,返回另一链表;
均非空,继续;
(2)确定新链表头结点,比较两链表头结点得出;
(3)依次遍历两链表,比较两链表各结点的值,小则加入新链表;
(4)当一链表到达尾结点,则结束循环,将另一链表直接加在新链表后面。
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2) {
if (!pHead1 && !pHead2)
return NULL;
ListNode *pHead = NULL;
ListNode *p = NULL, *p1 = pHead1, *p2 = pHead2;
ListNode *temp = NULL;
if (!pHead1)
return pHead2;
if (!pHead2)
return pHead1;
if (pHead1->val <= pHead2->val) {
p = pHead1;
p1 = pHead1->next;
}
else {
p = pHead2;
p2 = pHead2->next;
}
pHead = p;
pHead1 = NULL;
free(pHead1);
pHead2 = NULL;
free(pHead2);
while (p1 && p2) {
if (p1->val <= p2->val) {
temp = p1->next;
p->next = p1;
p = p->next;
p1 = temp;
}
else {
temp = p2->next;
p->next = p2;
p = p->next;
p2 = temp;
}
}
if (p1 && !p2)
p->next = p1;
if (!p1 && p2)
p->next = p2;
p = NULL;
free(p);
p1 = NULL;
free(p1);
p2 = NULL;
free(p2);
return pHead;
}
};
17.代码的鲁棒性:树的子结构
题目描述:
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
算法思路:
(1)首先设置标志位result = false,
若匹配成功result就设为true,剩下的代码不会执行;
若匹配不成功,默认返回false;
(2)递归思想,
若根节点相同则递归调用isSubTree();
若根节点不相同,则:
<1>判断tree1的左子树和tree2是否相同,
<2>再判断右子树和tree2是否相同;
(3)注意null的条件,
HasSubTree中,如果两棵树都不为空才进行判断;
isSubTree中,如果Tree2为空,则说明第二棵树遍历完了,即匹配成功,
tree1为空有两种情况:
<1>如果tree1为空&&tree2不为空说明不匹配,
<2>如果tree1为空,tree2为空,说明匹配。
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
bool isSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
if (!pRoot1 && pRoot2)
return false;
if (!pRoot2)
return true; // 子树遍历完成(关键语句)
if (pRoot1->val != pRoot2->val) //**不能再前两个判断之前,否则可能导致指针访问错误
return false;
bool result = true;
if (result)
result = isSubtree(pRoot1->left, pRoot2->left);
if (result)
result = isSubtree(pRoot1->right, pRoot2->right);
return result;
}
public:
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
bool result = false;
if (pRoot1 != NULL && pRoot2 != NULL) {
if (pRoot1->val == pRoot2->val)
result = isSubtree(pRoot1, pRoot2);
if (!result)
result = isSubtree(pRoot1->left, pRoot2);
if (!result)
result = isSubtree(pRoot1->right, pRoot2);
}
return result;
}
};
另解:
利用好短路特性
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
bool isSubtree(TreeNode* pRootA, TreeNode* pRootB) {
if (pRootB == NULL) return true;
if (pRootA == NULL) return false;
if (pRootB->val == pRootA->val) {
return isSubtree(pRootA->left, pRootB->left)
&& isSubtree(pRootA->right, pRootB->right);
}
else
return false;
}
public:
bool HasSubtree(TreeNode* pRootA, TreeNode* pRootB) {
if (pRootA == NULL || pRootB == NULL) return false;
return isSubtree(pRootA, pRootB) ||
HasSubtree(pRootA->left, pRootB) ||
HasSubtree(pRootA->right, pRootB);
}
};
18.面试思路:二叉树的镜像
题目描述:
操作给定的二叉树,将其变换为源二叉树的镜像。
输入描述:
二叉树的镜像定义:
解法思路:
从根结点开始,递归交换左右子树即可。
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
void Mirror(TreeNode *pRoot) {
if (!pRoot)
return;
TreeNode *temp;
temp = pRoot->left;
pRoot->left = pRoot->right;
pRoot->right = temp;
Mirror(pRoot->left);
Mirror(pRoot->right);
}
};
注意:
代码1:
class Solution {
public:
void Mirror(TreeNode *pRoot) {
TreeNode *temp;
if(pRoot) {
temp = pRoot->left;
pRoot->left = pRoot->right;
pRoot->right = temp;
}
if (pRoot->left)
Mirror(pRoot->left);
if (pRoot->right)
Mirror(pRoot->right);
}
};
代码1将报指针访问错误,因为递归调用的时候,只判断了pRoot->left和pRoot->right是否为空,却对pRoot未做判断,在使用指针时,切记对指针变量的判断。
修改为代码2即正确。
或者如下亦正确:
if (pRoot && pRoot->left)
Mirror(pRoot->left);
if (pRoot && pRoot->right)
Mirror(pRoot->right);
代码2:
class Solution {
public:
void Mirror(TreeNode *pRoot) {
TreeNode *temp;
if(pRoot) {
temp = pRoot->left;
pRoot->left = pRoot->right;
pRoot->right = temp;
Mirror(pRoot->left);
Mirror(pRoot->right);
}
}
};
19.画图让抽象形象化:顺时针打印矩阵
题目描述:
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10。
解法思路:
暴力输出
class Solution {
public:
vector<int> printMatrix(vector<vector<int> > matrix) {
vector<int> result;
if (matrix.empty())
return result;
int left = 0, right = matrix[0].size() - 1;
int top = 0, bottom = matrix.size() - 1;
while (left <= right && top <= bottom) {
for (int i = left; i <= right; ++i)
result.push_back(matrix[top][i]);
for (int i = top + 1; i <= bottom; ++i)
result.push_back(matrix[i][right]);
if (top != bottom)
for (int i = right - 1; i >= left; --i)
result.push_back(matrix[bottom][i]);
if (left != right)
for (int i = bottom - 1; i > top; --i)
result.push_back(matrix[i][left]);
left++, right--, top++, bottom--;
}
return result;
}
};
20.举例让抽象具体化:包含min函数的栈
题目描述:
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
class Solution {
public:
void push(int value) {
stackInt.push(value);
if (stackMin.empty())
stackMin.push(value);
else if (stackMin.top() < value)
stackMin.push(stackMin.top());
else
stackMin.push(value);
}
void pop() {
stackInt.pop();
stackMin.pop();
}
int top() {
return stackInt.top();
}
int min() {
return stackMin.top();
}
private:
stack<int> stackInt;
stack<int> stackMin;
};