输入一颗二元查找树,将该树转换为它的镜像
2016-05-11 17:27
369 查看
即在转换后的二元查找树中,左子树的结点都大于右子树的结点。
用递归和循环两种方法完成树的镜像转换。
例如输入:
8
/ /
6 10
/ / / /
5 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
};
用递归和循环两种方法完成树的镜像转换。
例如输入:
8
/ /
6 10
/ / / /
5 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 <stdio.h> typedef struct BSTreeNode // a node in the binary search tree (BST) { int nValue; // value of node struct BSTreeNode *pLeft; // left child of node struct BSTreeNode *pRight; // right child of node }BSTreeNode_T; void Revertsetree(BSTreeNode_T *pRoot) { if(NULL == pRoot) return; BSTreeNode_T *p = NULL; p = pRoot->pLeft; pRoot->pLeft = pRoot->pRight; pRoot->pRight = p; if(NULL != pRoot->pLeft) Revertsetree(pRoot->pLeft); if(NULL != pRoot->pRight) Revertsetree(pRoot->pRight); p = NULL; } int main(void) { return 0; }
相关文章推荐
- 如何配置树莓派3代的蓝牙连接
- Android API-23: InetAddressUtils replacement
- Style.xml的妙用(一)
- 多线程服务器的常用编程模型(笔记)
- hdu 1084 水题
- linux tar 打包 压缩 解压
- php PDO连接数据库
- 在首席架构师手里,应用架构如此设计
- Elasticsearch源码分析八--如何根据查询语句确定查询类型并解析查询语句
- 考勤情况记录数据表、短信发送记录表设计
- epoll源码分析(三)
- 第十二周项目一 阅读程序,请写出这些程序的运行结果(4)
- 如果二级控制器也是包含tabbar
- iCarousel效果
- SAP 创建工厂
- CSS重设(reset)方法总结
- xStream完美转换XML、JSON
- fragment
- Spring Boot + Gradle + Websocket 构建推送服务
- arcgis下载地址