求二元查找树的镜像
来源:互联网 发布:电脑视频文件修复软件 编辑:程序博客网 时间:2024/05/20 09:48
题目:输入一颗二元查找树,将该树转换为它的镜像,即在转换后的二元查找树中,左子树的结点都大于右子树的结点。用递归和循环两种方法完成树的镜像转换。
例如输入:
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<iostream>using namespace std;struct BSTreeNode // a node in the binary search tree (BST){int m_nValue; // value of nodeBSTreeNode *m_pLeft; // left child of nodeBSTreeNode *m_pRight; // right child of node};void create(BSTreeNode *&p,int *a,int i){if(i>7)return;p=new BSTreeNode;p->m_nValue=a[i];p->m_pLeft=p->m_pRight=NULL;create(p->m_pLeft,a,i*2);create(p->m_pRight,a,i*2+1);}void Turn(BSTreeNode *&p){if(p==NULL)return;BSTreeNode *temp;temp=p->m_pLeft;p->m_pLeft=p->m_pRight;p->m_pRight=temp;Turn(p->m_pLeft);Turn(p->m_pRight);}int main(){BSTreeNode *p;int a[]={0,8,6,10,5,7,9,11};create(p,a,1);Turn(p);return 0;}
0 0
- 求二元查找树的镜像
- 求二元查找树的镜像
- 求二元查找树的镜像
- 求二元查找树的镜像
- 求二元查找树的镜像
- 求二元查找树的镜像
- 求二元查找树的镜像
- 求二元查找树的镜像
- 求二元查找树的镜像
- 求二元查找树的镜像
- 求二元查找树的镜像
- 求二元查找树的镜像
- 求二元查找树的镜像
- 求二元查找树的镜像
- 求二元查找树的镜像 C++实现
- 求二元查找树树的镜像(剑指offer笔记)
- 程序员面试题精选(11):求二元查找树的镜像
- 程序员面试题精选100题(11)-求二元查找树的镜像
- vim配置
- 这几刚干完一个简单的项目,总结一下。本人是新手 第一次做项目,老手就不用看了
- 战略转移了,以后只在博客园上写博客了,欢迎来访。
- 学习笔记---赋值运算符与逗号运算符
- NS2 教學手冊 ( NS2 Learning Guide)
- 求二元查找树的镜像
- 从FILE_OBJECT里获取完整NT路径和DOS路径
- 超实用压力测试工具-ab工具
- 破解 jar 包之直接修改 .class 文件方式
- struts2框架如何使用websocket实现web程序消息推送
- LeetCode 219. Contains Duplicate II
- 个人记录-LeetCode 37. Sudoku Solver
- 使用编译好的OPENNI2的动态库与静态库进行Kinect2.0的ONI格式视频流录制
- Bebop2 FlightPlan 文件传输脚本结构