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?
https://leetcode.com/problems/recover-binary-search-tree/
二叉排序树中有两个节点被交换了,要求把树恢复成二叉排序树。
最简单的办法,中序遍历二叉树生成序列,然后对序列中排序错误的进行调整。最后再进行一次赋值操作,但这个不符合空间复杂度要求。
需要用两个指针记录错误的那两个元素,然后进行交换。
怎样找出错误的元素?遍历二叉排序树,正确时应该是从小到大,如果出现之前遍历的节点比当前大,则说明出现错误。所以我们需要一个pre指针来指向之前经过的节点。
如果只有一处不符合从小到大,则只用交换这一个地方。第二个指针记录第二个异常点。
Github repository: https://github.com/huashiyiqike/myleetcode
//JAVA CODE:
public class Solution {
TreeNode first = null, second = null, pre = null;
//first larger than follow, second smaller than pre
public void helper(TreeNode root){
if(root.left != null) helper(root.left);
if(pre != null && root.val < pre.val){
if(first == null)
first = pre;
second = root;
}
pre = root;
if(root.right != null) helper(root.right);
}
public void recoverTree(TreeNode root) {
helper(root);
int tmp = first.val;
first.val = second.val;
second.val = tmp;
}
}
//C++ CODE:
#include <iostream>
#include <cstdlib>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution {
TreeNode *first = NULL, *second = NULL, *last = NULL;
public:
void inorder(TreeNode* root){
if(root->left != NULL){
inorder(root->left);
}
if(last != NULL && root->val < last->val){
if(first == NULL) first = last;
second = root;
}
last = root;
if(root->right != NULL) inorder(root->right);
}
void recoverTree(TreeNode* root) {
if(root == NULL) return;
inorder(root);
if(first && second)
std::swap(first->val, second->val);
}
};
#PYTHON CODE:
class Solution:
def inorderTravel(self, root, last):
if root.left:
last = self.inorderTravel(root.left, last)
if last and last.val > root.val:
if self.first is None:
self.first = last
self.second = root
last = root
if root.right:
last = self.inorderTravel(root.right, last)
return last def recoverTree(self, root):
if root is None:
return
self.first, self.second = None, None
self.inorderTravel(root, None)
self.first.val, self.second.val = self.second.val, self.first.val
Recover Binary Search Tree--leetcode难题讲解的更多相关文章
- Recover Binary Search Tree [LeetCode]
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 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 ...
随机推荐
- Wix 安装部署教程(四) 添加安装文件及快捷方式
通过之前三篇的介绍,大家对wix的xml部署方式也应该有一些认识,今天天气不错,再来一发.主要介绍桌面,开始菜单,卸载等功能的如何添加.希望园友们支持! 一.如何添加文件 Demo打包程序很简单,就一 ...
- 如何在ASP.NET中用C#将XML转换成JSON
本文旨在介绍如果通过C#将获取到的XML文档转换成对应的JSON格式字符串,然后将其输出到页面前端,以供JavaScript代码解析使用.或许你可以直接利用JavaScript代码通过Ajax的方式来 ...
- Atitit.基于dsl的methodinvoker
Atitit.基于dsl的methodinvoker V2 new dyn invoke V3 plan Meth chain Prj cms methd_invok.bat rem a sta ...
- paip.解决 数据库mysql增加列 字段很慢添加字段很慢
paip.解决 数据库mysql增加列 字段很慢添加字段很慢 #环境如下: mysql5.6 数据仅仅3w alter table xxx add column yyy int default ...
- iOS开发----三目运算符
一.三目运算符 1.基本格式 : (关系表达式) ? 表达式1 : 表达式2; 执行流程 : 关系表达式为 真 返回表达式1 关系表达式为假 返回表达式2 2.写一个例子来看一下三目运算符的使用: ...
- iOS-项目打包为ipa文件
最近自己做的一个项目,由于app store发布流程比较复杂,且审核周期较长,客户希望提前能看到产品,所以我先给自己的项目打包成一个ipa文件(类似Android的apk安装包),然后发布在" ...
- 在php中,如何将一个页面中的标签,替换为用户想输出的内容
前言:釜山行,暴露人性, ———————————————————————————————————————————————————————————————————————————— 今天说一个最简单的例 ...
- Android 5.0/5.1开发问题专贴
注:非5.0特定的开发问题,可以在这个帖子里查:Android开发问题汇总. 1.官方提供的例子android-support-v7-appcompat编译时提示android:actionModeS ...
- GET请求中URL的最大长度限制总结
由于jsonp跨域请求只能通过get请求,url长度根据浏览器及服务器的不同而有不同限制. 若要支持IE的话,最大的长度为2083字符,若是中文字符的话只有2083/9=231个字符. 若是Chrom ...
- mac工具收藏
1.office字体兼容 http://mac.pcbeta.com/thread-32703-1-1.html