1. 二叉树的层序遍历
void _LevelOrder(Node *pRoot)
{
Node *pCur = pRoot;
queue<Node *> q;
q.push(pCur);
while (!q.empty())
{
Node *pTop = q.front();
cout << pTop->_data << " ";
q.pop();
if (pTop->_pLeft)
q.push(pTop->_pLeft);
if
2. 判断树是否是完全二叉树
bool _IsCompleteTree(Node *pRoot)
{
if (pRoot == nullptr)
return true;
queue<Node *> q;
Node *pCur = pRoot;
q.push(pCur);
bool flag = false;
while (!q.empty())
{
Node *pTop = q.front();
q.pop();
if (flag&&pTop->_pLeft || pTop->_pRight)
return false;
if (pTop->_pLeft == nullptr&&pTop->_pRight != nullptr)
return false;
if (pTop->_pLeft)
q.push(pTop->_pLeft);
if (pTop->_pRight)
q.push(pTop->_pRight);
else
flag = true;
}
return true;
}
3. 判断是否是二叉树的后续遍历
bool IsPostOrder(int sequence[], int length)
{
if (sequence == nullptr || length <= 0)
return false;
int root = sequence[length - 1];
int i = 0;
for (; i < length-1; ++i)
{
if (sequence[i]>root)
break;
}
int j = i;
for (; j < length - 1; ++j)
{
if (sequence[i] < root)
return false;
}
bool left = true;
if (i>0)
left = IsPostOrder(sequence, i);
bool right = true;
if (i < length - 1)
right = IsPostOrder(sequence + i, length - i - 1);
return
4. 打印二叉树中和为某一值的路径
void _FindPath(Node *pRoot, int expectData, int Sum, vector<int> &path)
{
Sum += pRoot->_data;
path.push_back(pRoot->_data);
bool isLeaf = pRoot->_pLeft == nullptr&&pRoot->_pRight == nullptr;
if (isLeaf&&Sum == expectData)
{
printf("find the path\n");
vector<int>::iterator it = path.begin();
for (; it != path.end(); it++)
{
printf("%d\t", *it);
}
printf("\n");
}
if (pRoot->_pLeft)
_FindPath(pRoot->_pLeft, expectData, Sum, path);
if (pRoot->_pRight)
_FindPath(pRoot->_pRight, expectData, Sum, path);
//递归退出时记得清理栈
5. 二叉搜索树转双向链表
//注意pre用引用
void _ToList(Node *pRoot, Node *&pre)
{
if (NULL==pRoot)
return;
Node *cur = pRoot;
_ToList(pRoot->_pLeft,pre);
if (pre)
{
pre->_pRight = cur;
}
cur->_pLeft = pre;
pre = cur;
_ToList(pRoot->_pRight,pre);
}
6. 复杂链表的复制
void CloneNode(ListNode *pHead)
{
ListNode *pNode = pHead;
while (pNode != nullptr)
{
ListNode *pCloned = new ListNode;
pCloned->_data = pNode->_data;
pCloned->_pNext = pNode->_pNext;
pCloned->_pSibling = nullptr;
pNode->_pNext = pCloned;
pNode = pCloned->_pNext;
}
}
void CloneSibling(ListNode *pHead)
{
ListNode *pNode = pHead;
while (pNode != nullptr)
{
ListNode *pCloned = pNode->_pNext;
if (pNode->_pSibling != nullptr)
{
pCloned->_pSibling = pNode->_pSibling->_pNext;
}
pNode = pCloned->_pNext;
}
}
ListNode *DivideList(ListNode *pHead)
{
ListNode *pNode = pHead;
ListNode *pCloneHead = nullptr;
ListNode *pCloneNode = nullptr;
if (pNode != nullptr)
{
pCloneHead = pNode->_pNext;
pNode->_pNext = pCloneHead->_pNext;
pNode->_pNext = pNode->_pNext;
}
while (pNode != nullptr)
{
pCloneNode->_pNext = pNode->_pNext;
pCloneNode = pCloneNode->_pNext;
pNode->_pNext = pCloneNode->_pNext;
pNode = pNode->_pNext;
}
return
7. 寻找数组中出现次数大于一半的数字
#include<iostream>
using namespace std;
bool flag = false;
int partion(int *arr, int length, int left, int right)
{
int key = arr[left];
int start = left;
int end = right - 1;
while (start < end)
{
while(start < end&&arr[end] >=key)
end--;
while (start < end&&arr[start] <= key)
++start;
if (start < end)
swap(arr[start], arr[end]);
}
swap(arr[start], arr[left]);
return start;
}
bool CheckLegal(const int *arr, int length)
{
flag = false;
if (arr == nullptr || length <= 0)
flag = true;
return flag;
}
bool resLegal(int *arr, int length, int res)
{
int times = 0;
for (int i = 0; i < length; i++)
{
if (arr[i] == res)
times++;
}
if (times * 2 < length)
return false;
return true;
}
int MoreThanNum(int *arr, int length)
{
if (CheckLegal(arr, length))
{
return 0;
}
int mid = length >> 1;
int start = 0;
int end = length - 1;
int index = partion(arr, length, start, end);
while (index != mid)
{
if (index > mid)
{
end = index - 1;
index = partion(arr, length, start, end);
}
else
{
start = index + 1;
index = partion(arr, length, start, end);
}
}
int res = arr[mid];
if (!resLegal(arr, length, res))
res = 0;
return res;
}
int Find(int *arr, int length)
{
if (arr == nullptr || length < 1)
return 0;
int res = arr[0];
int times = 0;
for (int i = 0; i < length; i++)
{
if (times == 0)
{
res = arr[i];
times = 1;
}
else if (arr[i] == res)
++times;
else
--times;
}
if (!resLegal(arr, length, res))
res = 0;
return res;
}
void test()
{
int arr[] = { 1, 4, 5, 2, 2, 2,2};
cout << Find(arr, sizeof(arr) / sizeof(arr[0]));
}
int main()
{
test();
system("pause");
}
8. 字符串全排列
#include<iostream>
using namespace std;
void range(char *str, char *pBegin)
{
if (*pBegin == '\0')
cout << str << endl;
else
{
for (char *pCh = pBegin; *pCh != '\0'; pCh++)
{
swap(*pCh, *pBegin);
range(str, pBegin + 1);
swap(*pCh, *pBegin);
}
}
}
int main()
{
char arr[] = "abc";
range(arr, arr);
system("pause");
return 0;
}