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 ...
随机推荐
- PDB调试Python程序
pdb是python内置的调试工具, 它可以在终端中调试Python程序, 这允许pdb在很多无法安装IDE的服务器上使用. 虽然远程调试使用广泛, 但在必要的时候(比如难以在本地搭建运行环境)pdb ...
- 在Visio2010中修改默认字体的大小
由于我常需要在Visio2010中画流程图和UML图,但是Visio2010中的字体默认8px,这对眼睛是个挑战.摸索了好久终于找到在visio2010中修改字体大小的方式. 1.点一下红色箭头所指的 ...
- JQuery高性能优化
使用JQuery时,你可以使用多种选择器,选择同一个元素,各种方法之间的性能是不一样的,有时候差异会特别大. 通常比较常用的选择器有以下几个: ID选择器 $("#id") 标签选 ...
- chrome浏览器扩展的事件处理
关于chrome扩展开发的栗子已经有很多了,问问度娘基本能满足你的欲望, 我想说的是扩展和页面间的数据传递问题. 我们知道写扩展有个必须的文件就是“manifest.json”, 这个里面定义了一个和 ...
- js/jq宽高的理解与运用
document:1. 与client相关的宽高document.body.clientWidthdocument.body.clientHeightdocument.body.clientLeftd ...
- Django网站管理、后台、模型(Admin)
1. 启动wampserver 2. 新建一个名为mysite的项目django-admin.py startproject mysite 3. 新建一个叫做blog的appcd mysitepyth ...
- Unreal Engine 4官网教程
编辑器纵览 https://www.unrealengine.com/zh-CN/blog/editor-overview 虚幻编辑器UI布局概述 https://www.unrealengine.c ...
- Advacned Puppet: Puppet Master性能调优
本文是Advanced Puppet系列的第一篇:Puppet master性能调优,谈一谈如何优化和提高C/S架构下master端的性能. 故事情节往往惊人地类似:你是一名使用Puppet管理线上业 ...
- Mysql 如何设置字段自动获取当前时间
应用场景: 1.在数据表中,要记录每条数据是什么时候创建的,不需要应用程序去特意记录,而由数据数据库获取当前时间自动记录创建时间: 2.在数据库中,要记录每条数据是什么时候修改的,不需要应用程序去特意 ...
- AWVS漏洞测试-01节-AWVS的主要作用
AWVS漏洞工具简单介绍 AWVS全称: Acunetix Web Vulnerability Scanner 中文翻译就是:Acunetix网站攻击扫描器 扫描网站漏洞,通过网络爬虫Crawler的 ...