前序

文章目录

        • 前序
        • 中序
        • 冒泡
        • 快排
        • 直插

前序

vector preorder_search(TreeNode* root)
{
	if (root == NULL) return res;
	TreeNod *node = root;
	stack s;
	while(node!=nullptr||!s.empty())
	{
		while(node!=nullptr)
		{
			/* operation of node */
			s.push(node);
			node = node->left;
		}
		if(!s.empty())
		{
			p = s.top();
			s.pop();
			node = node->right;
		}
	}
}

中序

vector middle_search(TreeNode* root)
{
	if (root == NULL) return res;
	TreeNod *node = root, *pre_visited = nullptr;
	stack s;
	while(node!=nullptr||!s.empty())
	{
		while(node!=nullptr)
		{
			s.push(node);
			node = node->left;
		}
		node = s.top();
		if(node->right==nullptr||node->right==pre_visited)
		{
			/* operation of node */
			s.pop();
			pre_visited = node;
			node = nullptr;
		}
		else
		{
			node = node->right;
		}
	}
}

冒泡

void Bubblesort(vector &num)
{
    if(num.empty())
        return;
    for(int i=0; inum[j+1])
            {
                flag = false;
                swap(num[j], num[j+1]);
            }
        if(flag)
            break;
    }
}

快排

void QuickSort(vector &num, int start, int end)
{
    if(num.size()<=1||end<=start)
		return;
    int base = start;
    int left = base, right = end;
    while(left=num[base])
            right--;
        while(left

直插

void InsertSort(vector &num)
{
	if(num.empty())
		return;
	for(int i=1; i0&&num[j]

你可能感兴趣的:(校招复习,数据结构&算法)