二叉树的相关面试题(一)

1.创建二叉树

在我写的另一派博客中:https://blog.csdn.net/Damn_Yang/article/details/84728761

2.前/中/后序遍历二叉树(递归与非递归)

前序(递归与非递归):

//递归
void BinaryTreePrevOrder(BTNode* root)
{
	if (root == NULL)
	{
		return;
	}
	printf("%c ", root->_data);
	BinaryTreePrevOrder(root->_left);
	BinaryTreePrevOrder(root->_right);

}
//非递归
void BinaryTreePrevOrderNonR(BTNode* root)
{
	BTNode* cur = root;
	Stack s;
	BTNode* top;
	StackInit(&s);
	while (cur || StackEmpty(&s) != 0)
	{
		//访问左路节点 左路节点入栈
		while (cur)
		{
			printf("%c ", cur->_data);
			StackPush(&s, cur);
			cur = cur->_left;
		}
		//栈里面出的节点 表示左树已经访问过了
		top = StackTop(&s);
		StackPop(&s);
		//子问题访问右树
		cur = top->_right;
	}
}

中序(递归与非递归)

//中序遍历
//递归
void BinaryTreeInOrder(BTNode* root)
{
	if (root == NULL)
	{
		return;
	}
	BinaryTreeInOrder(root->_left);
	printf("%c ", root->_data);
	BinaryTreeInOrder(root->_right);


}
//非递归
void BinaryTreeInOrderNonR(BTNode* root)
{
	BTNode* cur = root;
	Stack s;
	BTNode* top;
	StackInit(&s);
	while (cur || StackEmpty(&s) != 0)
	{
		while (cur)
		{
			StackPush(&s, cur);
			cur = cur->_left;
		}
		//左树已经访问过了 还剩右树
		top = StackTop(&s);
		StackPop(&s);
		printf("%c ", top->_data);
		cur = top->_right;
	}
}

后序(递归与非递归)

//后序遍历
//递归
void BinaryTreePostOrder(BTNode* root)
{
	if (root == NULL)
	{
		return;
	}
	BinaryTreePostOrder(root->_left);
	BinaryTreePostOrder(root->_right);
	printf("%c ", root->_data);


}
//非递归
void BinaryTreePostOrderNonR(BTNode* root)
{
	BTNode* cur = root;
	BTNode* top;
	BTNode* prev = NULL;
	Stack s;
	StackInit(&s);
	while (cur || StackEmpty(&s) != 0)
	{
		while (cur)
		{
			StackPush(&s, cur);
			cur = cur->_left;
		}
		top = StackTop(&s);
		if (top->_right == NULL || top->_right == prev)
		{
			printf("%c ", top->_data);
			prev = top;
			StackPop(&s);
		}
		else
		{
			cur = top->_right;
		}
	}
}

3.层序遍历二叉树

void BinaryTreeLevelOrder(BTNode* root)
{
	Queue q;
	QueueInit(&q);
	if (root)
	{
		QueuePush(&q, root);
	}
	while (QueueEmpty(&q) != 0)
	{
		DataType front = QueueFront(&q);
		printf("%c ", front->_data);
		QueuePop(&q);
		if (front->_left)
		{
			QueuePush(&q, front->_left);
		}
		if (front->_right)
		{
			QueuePush(&q, front->_right);
		}
	}
}

4.求二叉树的高度

int BinaryTreeHigh(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	else
	{
		return BinaryTreeHigh(root->_left) >= BinaryTreeHigh(root->_right) ?
			BinaryTreeHigh(root->_left) + 1 : BinaryTreeHigh(root->_right) + 1;
	}
}

5.求二叉树中节点的个数

int BinaryTreeSize(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	else
	{
		return BinaryTreeSize(root->_left) + BinaryTreeSize(root->_right) + 1;
	}
}

6.求叶子节点的个数

int BinaryTreeLeafSize(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	if (root->_left == NULL && root->_right == NULL)
	{
		return 1;
	}
	return BinaryTreeLeafSize(root->_left) + BinaryTreeLeafSize(root->_right);
}

7.求二叉树中第K层节点的个数

int BinaryTreeLevelKSize(BTNode* root, int k)
{
	if (root == NULL)
	{
		return 0;
	}
	if (k == 1)
	{
		return 1;
	}
	return BinaryTreeLevelKSize(root->_left, k - 1) + BinaryTreeLevelKSize(root->_right, k - 1);
}

8.判断一颗二叉树是否在一颗二叉树中

BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
	BTNode* ret = 0;
	if (root = NULL)
	{
		return 0;
	}
	if (root->_data == x)
	{
		return root;
	}
	ret = BinaryTreeFind(root->_left, x);
	if (ret)
	{
		return ret;
	}
	ret = BinaryTreeFind(root->_right, x);
	if (ret)
	{
		return ret;
	}
	return NULL;
}

9.求二叉树中两个节点的最近公共祖先节点

//两个结点的最近公共祖先
BTNode* lowestCommonAncestor(BTNode* root, BTNode* p, BTNode* q)
{
	if (root == NULL || root == p || root == q)
	{
		return root;
	}
	BTNode* left = lowestCommonAncestor(root->_left, p, q);
	BTNode* right = lowestCommonAncestor(root->_right, p, q);
	if (left && right)
	{
		return root;
	}
	else
	{
		return left == NULL ? right : left;
	}
}

10.判断一颗二叉树是否为完全二叉树

int BinaryTreeComplete(BTNode* root)
{
	Queue q;
	QueueInit(&q);
	BTNode*front;
	if (root)
	{
		QueuePush(&q, root);
	}
	while (QueueEmpty(&q))
	{
		front = QueueFront(&q);
		QueuePop(&q);
		if (front)
		{
			QueuePush(&q, front->_left);
			QueuePush(&q, front->_right);
		}
		else
		{
			break;
		}
	}
	while (QueueEmpty(&q))
	{
		BTNode*front = QueueFront(&q);
		if (front)
		{
			QueueDestory(&q);
			return -1;
		}
		else
		{
			QueuePop(&q);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Damn_Yang/article/details/84780722