LeetCode OJ-- Recover Binary Search Tree ***@
https://oj.leetcode.com/problems/recover-binary-search-tree/
一棵二叉搜索树,二叉搜索树的特征是,中根遍历的话,得到的序列是递增的
题目中,有两个节点弄混了,让恢复这个二叉搜索树
class Solution {
public:
TreeNode *LastNode = new TreeNode(INT_MIN); void recoverTree(TreeNode *root) {
if(root == NULL)
return; TreeNode **n1 = (TreeNode**)malloc(sizeof(TreeNode*));
TreeNode **n2 = (TreeNode**)malloc(sizeof(TreeNode*));
*n1 = NULL;
*n2 = NULL;
find(root,n1,n2);
//swap
int temp;
temp = (*n1)->val; (*n1)->val = (*n2)->val; (*n2)->val = temp;
} //中根遍历
void find(TreeNode *root, TreeNode **n1, TreeNode **n2)
{
if(root == NULL)
return; find(root->left,n1,n2);
//分别处理了是 2 1 还是,3 2 1 的情况,高!
if(*n1 == NULL && root->val < LastNode->val)
{
*n1 = LastNode;
}
if(*n1 != NULL && root->val < LastNode->val)
{
*n2 = root;
} LastNode = root; find(root->right,n1,n2);
}
};
LeetCode OJ-- Recover Binary Search Tree ***@的更多相关文章
- [LeetCode] 99. Recover Binary Search Tree(复原BST) ☆☆☆☆☆
Recover Binary Search Tree leetcode java https://leetcode.com/problems/recover-binary-search-tree/di ...
- 【leetcode】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [Leetcode][JAVA] Recover Binary Search Tree (Morris Inorder Traversal)
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- leetcode 99 Recover Binary Search Tree ----- java
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [leetcode]99. Recover Binary Search Tree恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- Java for LeetCode 099 Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- LeetCode OJ——Validate Binary Search Tree
http://oj.leetcode.com/problems/validate-binary-search-tree/ 判断一棵树是否为二叉搜索树.key 是,在左子树往下搜索的时候,要判断是不是子 ...
- Leetcode#99 Recover Binary Search Tree
原题地址 中序遍历二叉搜索树,正常情况下所有元素都应该按递增排列,如果有元素被交换,则会出现前面元素大于后面的情况,称作反序.由于交换了两个节点,所以通常会有两处反序,但如果是两个相邻节点发生了交换, ...
随机推荐
- ActiveMQ发布-订阅消息模式(同点对点模式的区别)
点对点与发布订阅最初是由JMS定义的.这两种模式主要区别或解决的问题就是发送到队列的消息能否重复消费(多订阅) 点对点: 消息生产者生产消息发送到queue中,然后消息消费者从queue中取出并且消费 ...
- 动态规划:HDU1789-Doing Homework again
Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- poj 2533Longest Ordered Subsequence
Longest Ordered Subsequence Description A numeric sequence of ai is ordered if a1 < a2 < - < ...
- DevOps实施的三种IT障碍
[TechTarget中国原创] 现今DevOps可谓是红遍半边天,但正因为它是新的东西,企业也在不停的犯同样的错误.从这些挑战中学习,让你的DevOps项目取得成功. DevOps正在以一种更有效的 ...
- 微服务学习笔记——Spring Boot特性
1. 创建独立的Spring应用程序 2. 嵌入的Tomcat,无需部署WAR文件 3. 简化Maven配置 4. 自动配置Spring 5. 提供生产就绪型功能,如指标,健康检查和外部配置 6. 开 ...
- ACM-ICPC 2018 南京赛区网络预赛
轻轻松松也能拿到区域赛名额,CCPC真的好难 An Olympian Math Problem 问答 只看题面 54.76% 1000ms 65536K Alice, a student of g ...
- 在数组中寻找出现次数大于N/K的数
给定一个int[]数组,给定一个整数k,打印所有出现次数大于N/k的数,没有的话,给出提示信息. === 核心思想:一次在数组中删除K个不同的数,不停的删除,直到剩下的数的种类不足K就停止删除,那么如 ...
- Spring Cloud 目录
Spring Cloud Eureka Spring Cloud Config Spring Cloud Feign Spring Cloud Hystrix Spring Cloud Ribbon ...
- JS获取屏幕宽度
document.write("屏幕分辨率为:"+screen.width+"*"+screen.height+"<br />" ...
- 欧拉回路基础 HDU1878 欧拉回路||并差集
欢迎参加——每周六晚的BestCoder(有米!) 欧拉回路 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...