Recover Binary Search Tree [LeetCode]
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
Note:
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?
Sulotion: Considers a BST as a storted array, so we can traverse the BST inorderly just like iterate the ascending sequence. In this way, we can easily find two elements swapped by mistake, then swaps them back.
void inorder(TreeNode * root, TreeNode ** error_node, TreeNode ** pre_node, int * is_swap){
if( root == NULL )
return;
if(root->left != NULL)
inorder(root -> left, error_node, pre_node, is_swap);
if((*pre_node) != NULL) {
if ((*pre_node)->val > root->val){
if ((*error_node) == NULL)
*error_node = *pre_node;
}else if( (*error_node) != NULL && (*error_node)->val < root->val && (*error_node)->val > (*pre_node )->val){
//swap error node and pre node
int tmp = (*error_node)->val;
(*error_node )->val = (*pre_node)->val;
(*pre_node)->val = tmp;
*is_swap = ;
return;
}
}
(*pre_node )= root;
if( root -> right != NULL)
inorder(root -> right, error_node, pre_node, is_swap);
}
void recoverTree(TreeNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
TreeNode * error_node = NULL;
TreeNode * pre_node = NULL;
int is_swap = ;
inorder(root, &error_node, &pre_node, &is_swap);
if ( is_swap == && error_node != NULL){
int tmp = error_node -> val;
error_node -> val = pre_node -> val;
pre_node -> val = tmp;
}
}
Recover Binary Search Tree [LeetCode]的更多相关文章
- Recover Binary Search Tree leetcode java
题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- [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,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- 【leetcode】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- 【LeetCode练习题】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- 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
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
随机推荐
- 以一则LUA实例说明敏捷开发中“分离构造和使用”原则
分离构造和使用 构造含义是功能的实现, 此功能是一个定义明确的处理过程, 开放出明确的接口给调用者使用. 则使用者可以直接调用接口进行使用, 但是使用者需要搞清楚, 那些是构造, 那些是使用. 不要再 ...
- 移动端html页面布局
记录一下: ---------- 如果设计稿尺寸为 : 96px: 1. rem html{ font-size:62.5%; } 的情况下 : 4.8rem: 2. rem html{ font-s ...
- 小Q系列之 最佳裁判
这个题需要注意一些数据条件 尤其是一些输入数据条件 #include<algorithm> #include<stdio.h> #include<math.h> u ...
- .NET 集合类型性能分析
集合 Add Insert Remove Item Sort Find List<T> 如果集合重置大小,就是O(1)或O(n) O(n) O(n) O(1) O(n log n),最坏情 ...
- JQuery基础二
1.表单过滤器 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- Extract Stylish styles and save as JSON format
Introduction Stylish is a easy browser extension/plugin for users to customizing the web page stylin ...
- MVC系列1-MVC基础
终于决定写一个系列的文章了,最开始其实是准备写一下WPF的,因为我这两年一直在做WPF,对WPF的喜爱自然是无以言表.但是由于我所在的地区对WPF的普及不是很广泛,所以,被迫又开始做起来web,但是我 ...
- nwjs如何打包文件为exe文件并修改exe图标
1.下载nw.js,如果是SDK版的可以调试页面,打包后可不可以调试还没有试,不是SDK的话没有调试选项,试了一下,打包后的文件也一样调试不了. 2.把要打包的文件和package.json都放在nw ...
- jquery幻灯片
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 2016年江西理工大学C语言程序设计竞赛(高级组)
问题 A: jxust 解法:争议的问题(是输入整行还是输入字符串),这里倾向输入字符串,然后判断是否含有jxust就行 #include<bits/stdc++.h> using nam ...