# 剑指Offer前五章题目整合及答案

#### 6.从尾到头打印链表

``````struct ListNode {
int m_value;
ListNode* m_pNext;
}
``````

#### 7.重建二叉树

``````struct BinaryTreeNode
{
int m_value;
BinaryTreeNode m_pLeft;
BinaryTreeNode m_pRight;
}
``````

#### 9.用两个栈实现队列

``````template <typename T> class CQueue
{
public:
CQueue(void);
~CQueue(void);

void appendTail(const T& element);
T delegeHead();

private:
stack<T> stack1;
stack<T> stack2;
};
``````

a b t g
c f c s
j d e h

#### 18.删除链表节点

##### 题目1 在o(1)时间内删除链表节点

``````struct ListNode {
int m_value;
ListNode* m_pNext;
}
void deleteNode(ListNode** pHeader, ListNode *pToBeDeleted);
``````
##### 题目2 删除链表中重复的节点

• (a) 1 -> 2 -> 3 -> 3 -> 4 -> 4 -> 5
• (b) 1 -> 2 -> 5

#### 22.链表中倒数第K个节点

``````struct ListNode {
int m_value;
ListNode* m_pNext;
}
``````

#### 24.反转链表

``````struct ListNode {
int m_value;
ListNode* m_pNext;
}
``````

#### 25.合并两个排序的链表

``````struct ListNode {
int m_value;
ListNode* m_pNext;
}
``````
• 链表1：1 --> 3 --> 5 --> 7
• 链表2：2 --> 4 --> 6 --> 6
• 链表3：1 --> 2 --> 3 --> 4 --> 5 --> 6 --> 7 --> 8

#### 26.树的子结构

``````struct BinaryTreeNode
{
int m_value;
BinaryTreeNode m_pLeft;
BinaryTreeNode m_pRight;
}
``````

#### 27.二叉树的镜像

``````struct BinaryTreeNode
{
int m_value;
BinaryTreeNode m_pLeft;
BinaryTreeNode m_pRight;
}
``````

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

#### 32.从上到下打印二叉树

##### 题目1：不分行从上到下打印二叉树

``````struct BinaryTreeNode
{
int m_value;
BinaryTreeNode m_pLeft;
BinaryTreeNode m_pRight;
}
``````

#### 34.二叉树中和为某一值的路径

``````struct BinaryTreeNode
{
int m_value;
BinaryTreeNode m_pLeft;
BinaryTreeNode m_pRight;
}
``````

#### 35.复杂链表的复制

struct ListNode
{
int m_value;
ListNode* m_pNext;
ListNode* m_pSibling
};

#### 36.二叉搜索树与双向链表

struct BinaryTreeNode
{
int m_value;
BinaryTreeNode m_pLeft;
BinaryTreeNode m_pRight;
}

#### 52.两个链表的第一个公共节点

``````struct ListNode {
int m_value;
ListNode* m_pNext;
}
``````