第15 题: 题目:输入一颗二元查找树,将该树转换为它的镜像,

即在转换后的二元查找树中,左子树的结点都大于右子树的结点。用递归和循环两种方法完成树的镜像转换。

例如输入: 

        8
       / \
      6 10
      /\ /\
    7 9 11

输出: 

          8
      /     \
   10     6
     /\      /\
  11 9 7 5

定义二元查找树的结点为:

struct BSTreeNode // a node in the binary search tree (BST)
{
int m_nValue; // value of node
BSTreeNode *m_pLeft; // left child of node
BSTreeNode *m_pRight; // right child of node
};


分析:镜像转换无非就是对每个节点进行一个左右交换。

#include <iostream>
#include <queue>

using namespace std;

struct BNode
{
	int key;
	BNode *m_pl;
	BNode *m_pr;
};

typedef BNode* T;
typedef queue<T> QET;

void CreateTree(BNode* &head)
{
	QET qe;

	head = new BNode();
	cin >> head->key;
	head->m_pl = head->m_pr = 0;
	qe.push(head);

	while ( !qe.empty() )
	{
		BNode *temp = qe.front();
		qe.pop();
		cout << temp->key << "的左右子女的关键字,空用-1表示:" << endl;
		
		BNode *l = new BNode();
		BNode *r = new BNode();

		cin >> l->key >> r->key ;
		if (l->key == -1)
			delete l;
		else 
		{
			l->m_pl = l->m_pr = 0;
			temp->m_pl = l;
			qe.push(l);
		}
			
		if (r->key == -1)
			delete r;
		else
		{
			r->m_pl = r->m_pr = 0;
			temp->m_pr = r;
			qe.push(r);
		}
	}
	return ;
}

void RecurWay(BNode *head)
{
	if ( head == 0 )
		return ;

	RecurWay(head->m_pl);
	RecurWay(head->m_pr);
	
	BNode *s = head->m_pl;
	head->m_pl = head->m_pr;
	head->m_pr = s;

	return ;
}

void CircuWay(BNode *head)
{
	QET qe;
	
	if ( head == 0 )
		return ;

	qe.push(head);
	while ( !qe.empty() )
	{
		BNode *top = qe.front();

		if ( top->m_pl )
			qe.push(top->m_pl);
		if ( top->m_pr )
			qe.push(top->m_pr);

		BNode *s = top->m_pl;
		top->m_pl = top->m_pr;
		top->m_pr = s;

		qe.pop();
	}

	return ;
}

int main()
{
	BNode *head = 0;

	CreateTree(head);	
	
	RecurWay(head);
	CircuWay(head);

	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二元查找也被称为二叉搜索,是一种特殊的二叉,其子树中所有节点的值小于根节点的值,右子树中所有节点的值大于根节点的值。后序遍历的顺序是:子树 -> 右子树 -> 根节点。 判断一个整数序列是否为二元查找的后序遍历,可以分为以下步骤: 1. 读入整数序列,存储到数组中。 2. 判断数组是否为空或者长度为0,如果是则不是二元查找的后序遍历,直接返回 false。 3. 找到根节点,即数组的最后一个元素。 4. 在数组中找到第一个大于根节点的元素,其位置为分界点,分界点边的元素都属于子树,右边的元素都属于右子树。 5. 递归判断子树和右子树是否也是二元查找的后序遍历。 下面是一份示例代码: ```c #include <stdio.h> #include <stdbool.h> bool is_bst_postorder(int *nums, int length) { if (nums == NULL || length <= 0) { return false; } // 找到根节点 int root = nums[length - 1]; // 找到分界点 int i; for (i = 0; i < length - 1; i++) { if (nums[i] > root) { break; } } // 判断右子树中是否存在小于根节点的元素 int j; for (j = i; j < length - 1; j++) { if (nums[j] < root) { return false; } } // 递归判断子树和右子树 bool left = true, right = true; if (i > 0) { left = is_bst_postorder(nums, i); } if (i < length - 1) { right = is_bst_postorder(nums + i, length - i - 1); } return left && right; } int main() { int nums[] = {1, 3, 2, 5, 7, 6, 4}; int length = sizeof(nums) / sizeof(int); if (is_bst_postorder(nums, length)) { printf("The integer sequence is a binary search tree's postorder traversal.\n"); } else { printf("The integer sequence is not a binary search tree's postorder traversal.\n"); } return 0; } ``` 输出结果为: ``` The integer sequence is a binary search tree's postorder traversal. ``` 注意,在判断右子树中是否存在小于根节点的元素时,应该从分界点开始遍历,而不是从根节点的位置开始遍历。因为右子树的元素一定在根节点的右边,而不是紧挨着根节点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值