恢复被交换二叉搜索树的节点值
来源:互联网 发布:python 扑克 编辑:程序博客网 时间:2024/06/08 13:10
#include <iostream>#include <vector>#include<algorithm>using namespace std;vector<int> G;//数据类class Data {public:int m, n;Data(int m, int n) {this->m = m;this->n = n;}};struct BTNode{int m_value;BTNode *m_left;BTNode *m_right;};//先序创建二叉树void CreatBTree(BTNode *&root){int nValue;cin >> nValue;if (0 == nValue){return;}else{root = new BTNode();root->m_value = nValue;CreatBTree(root->m_left);CreatBTree(root->m_right);}}void Search1(BTNode *pRoot)//将数字压到G中{if (pRoot != NULL) {Search1(pRoot->m_left);G.push_back(pRoot->m_value);Search1(pRoot->m_right);}}Data *Search() {int length = G.size();int m, n;for (int i = 0; i < length; i++) {if (i == 0 && G[i] > G[i + 1]) {m = i;}else {if (i != 0 && i < length - 1 && (G[i - 1]<G[i] && G[i]>G[i + 1])) {m = i;}else {if (i != 0 && i < length - 1 && (G[i - 1] > G[i] && G[i] < G[i + 1])) {n = i;}else {if (i == length - 1 && G[i] < G[i - 1]) {n = i;}}}}}return new Data(m, n);}void InOrder(BTNode *&pRoot, Data *data) {if (pRoot != NULL) {InOrder(pRoot->m_left, data);if (pRoot->m_value == G[data->m]) {pRoot->m_value = G[data->n];}else {if (pRoot->m_value == G[data->n]) {pRoot->m_value = G[data->m];}}InOrder(pRoot->m_right, data);}}void InOrder1(BTNode *&pRoot, Data *data) {if (pRoot != NULL) {InOrder(pRoot, data);cout << pRoot->m_value << " ";InOrder(pRoot, data);}}void Print(BTNode *&pRoot) {if (pRoot != NULL) {Print(pRoot->m_left);cout << pRoot->m_value << " ";Print(pRoot->m_right);}}int main(){BTNode *pRoot = NULL;CreatBTree(pRoot);Search1(pRoot);Data *data = Search();//InOrder1(pRoot,data);InOrder(pRoot, data);Print(pRoot);//Print1();system("pause");return 0;}
1 0
- 恢复被交换二叉搜索树的节点值
- 每天一道LeetCode-----二叉搜索树的某两个节点被交换位置,修正这个二叉搜索树
- 二叉搜索树(BST)的两个元素被错误地交换。 恢复树,而不改变它的结构。
- LeetCode 重构二叉搜索数,即找出两个被交换的节点
- 交换二叉树左右节点
- 二叉树交换左右节点
- 恢复二叉搜索树
- 恢复二叉搜索树
- leetcode 99. Recover Binary Search Tree BST二叉搜索树的两元素交换的恢复 + 中序遍历
- 二叉搜索树的根节点插入
- 搜索二叉树的错误节点
- 二叉搜索树的节点插入,查找。
- 找出二叉搜索树的最大节点和最小节点
- 求二叉搜索树任一节点的前驱后继节点
- 【LeetCode91-100】编码种数,逆转单链表,IP地址转化,中序遍历二叉树,生成二叉搜索树,计算二叉树个数,交叉string【hard】,判断二叉搜索树是否合法,恢复二叉树(有两个元素被交换)
- 非递归实现交换二叉树的左右子节点
- 交换二叉树中每个节点的两个子女
- lintcode--二叉搜索树交换节点(leetcode--Recover Binary Search Tree)
- 【SSH网上商城项目实战16】Hibernate的二级缓存处理首页的热门显示
- eclipse中获得html5标签提示
- java中“==”和euqals的区别
- 关于设备环境的获取方法
- java中的DAO设计模式
- 恢复被交换二叉搜索树的节点值
- Android View 事件分发机制 源码解析
- MySQL关闭查询缓存(QC)的两种方法
- linux下查看负载均衡的两种方法
- 安卓图案解锁插件
- struts2分页查询
- hpuacm 第一次省赛选拔
- 强强联手:诚迈科技携手研华共同发起嵌入式Linux和Android联盟
- 优化dijkstra算法