师兄面试总结编程部分解答之四

个人感觉树的问题大部分都是用递归的思想。

首先定义树节点结构体

typedef struct _treeNode
{
	int val;
	_treeNode* left;
	_treeNode* right;
	_treeNode (int value)
	{
		val = value;
		left = NULL;
		right = NULL;
	}
}TreeNode,*TreeNodePtr;

   构造搜索二叉树:

//根据输入内容构造二叉查找
TreeNodePtr constructTree()
{
	int c;
	TreeNodePtr parentNode,root,curNode;
	std::cin>>c;
	root = new TreeNode(c);
	parentNode = root;
	curNode = root;
	while((std::cin>>c))
	{
		curNode = root;
		while(curNode)
		{
			if (c < curNode->val)
			{
				parentNode = curNode;
				curNode = curNode->left;
			}else
			{
				parentNode = curNode;
				curNode = curNode->right;
			}
		}
		TreeNodePtr tmpNode = new TreeNode(c);
		if(c < parentNode->val)
			parentNode->left = tmpNode;
		else
			parentNode->right = tmpNode;
	}
	return root;
}

1、树的深度,广度遍历源代码

//使用堆栈
//深度优先遍历,非递归算法实现
//代码书写熟练程度远远不够,还要进行多次调试,说明思路不清晰
void DFS(TreeNodePtr root)
{
	if(root == NULL)
		return;
	TreeNodePtr stack[MAX];
	int top = 0;
	TreeNodePtr cur = root;
	stack[top++] = cur;
	cur = cur->left;
	while(top != 0 || cur != NULL)
	{
		while(cur != NULL)
		{
			stack[top++] = cur;
			cur = cur->left;
		}
		if(top > 0)
		{
			cur = stack[--top];
			std::cout<<cur->val<<" ";
			cur = cur->right;
		}
	}
	std::cout<<std::endl;
}
//使用队列
//广度优先遍历
void BFS(TreeNodePtr root)
{
	if (root == NULL)
		return;
	TreeNodePtr queue[MAX];
	int head = 0;
	int tail = 0;
	queue[head++] = root;
	TreeNodePtr cur;
	while(tail != head)
	{
		cur = queue[tail++];
		std::cout<<cur->val<<" ";
		if(cur->left)
			queue[head++] = cur->left;
		if(cur->right)
			queue[head++] = cur->right;
	}
	std::cout<<std::endl;
}

2、二叉树镜像(剑指offer125页)

//将给定树,转化为其镜像,左右互换
void change(TreeNodePtr root)
{
	if(root == NULL)
		return;
	TreeNodePtr tmp = root->left;
	root->left = root->right;
	root->right = tmp;
}
void TreeMirror(TreeNodePtr root)
{
	//思路利用广度优先遍历,并分别互换子树
	if(root == NULL)
		return;
	TreeNodePtr queue[MAX];
	int head = 0;
	int tail = 0;
	queue[head++] = root;
	TreeNodePtr cur;
	while(tail != head)
	{
		cur = queue[tail++];
		change(cur);
		if(cur->left)
			queue[head++] = cur->left;
		if(cur->right)
			queue[head++] = cur->right;
	}
}
//二叉树镜像递归实现
void TreeMirror_recu(TreeNodePtr root)
{
	if (root == NULL)
		return;
	TreeNodePtr tmp = root->left;
	root->left = root->right;
	root->right = tmp;
	TreeMirror_recu(root->left);
	TreeMirror_recu(root->right);
}

3、二叉树中某一值的路径(剑指offer 143页)

//在二叉树中找出和为某一值的所有路径 
//找到路径还是比较简单的,关键是打印路径怎么去实现
bool isLeaf(TreeNodePtr root)
{
	if (!root) return false;
	if(root->left == NULL && root->right == NULL)
		return true;
	else
		return false;
}
void findPath(TreeNodePtr root, int sum,TreeNodePtr stack[],int top)
{

	if(root == NULL) return;
	if(isLeaf(root) && root->val == sum)
	{
		std::cout<<"find a path: ";
		for(int i = 0; i < top;i++)
		{
			std::cout<<stack[i]->val<<" ";
		}
		std::cout<<root->val;
		std::cout<<std::endl;
		return;
	}
	if( !isLeaf(root) && root->val < sum)
	{
		stack[top++] = root;
		if (root->left)
			findPath(root->left, sum - root->val,stack,top);
		if(root->right)
			findPath(root->right, sum - root->val,stack,top);
	}
	else
		return;
		
}

4、二叉搜索树后序遍历序列(剑指offer 140页)

//后序遍历,这里的顺序是只根节点的遍历顺序,后序即为 左子树-右子树-根节点 顺序完成遍历
void LRD(TreeNodePtr root)
{
	if (!root) return;
	TreeNodePtr stack[MAX];
	int record[MAX] = {0};
	int top = 0;
	TreeNodePtr cur = root;
	while(top != 0 || cur != NULL)
	{
		while(cur != NULL)//左支深度遍历
		{
			stack[top] = cur;
			record[top++] = 1;
			cur = cur->left;
		}
		cur = stack[top-1];
		if(cur->right == NULL || record[top - 1] == 2)//当前节点右子树为空,或已经二次遍历
		{
			std::cout<<cur->val<<" ";
			top--;
			cur = NULL;
		}
		else//遍历右子树
		{
			record[top - 1] = 2;
			cur = cur->right;
		}
	}
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值