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?


题解:需要找到二叉搜索树中乱序的两个节点,并把它们交换回来。

排序二叉树的中序遍历得到的一定是一个递增序列,例如上述所示的BST,中序遍历得到的序列为1,3,5,9,11,15,20。假设错位的是3和11,那么错位后的树如下图所示

用遍历firstNode和secondNode表示错位的两个点,那么在中序遍历过程中,第一个错位点后面的点一定比它小(5比11小,11才是错位的点);第二个错位点一定比它前面的点小(3比9小)。采用递归的方法中序遍历BST,如果当前遍历的点root比它前面的点prev的值小,有两种可能,第一种是prev是第一个错位的点,此时firstNode变量为空,则将firstNode赋值为prev(代码中第24行);第二种是root是第二个错位的点,此时firstNode变量部位空,将secondNode变量赋值为root(代码中第22行)。

代码如下:

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private TreeNode firstNode = null;
private TreeNode secondNode = null;
private TreeNode prev = null; private void traverse(TreeNode root){
if(root == null)
return; traverse(root.left); if(prev != null && root.val < prev.val){
secondNode = root;
if(firstNode == null)
firstNode = prev;
}
prev = root;
traverse(root.right); } public void recoverTree(TreeNode root) {
traverse(root); int temp = firstNode.val;
firstNode.val = secondNode.val;
secondNode.val = temp;
}
}

【leetcode刷题笔记】Recover Binary Search Tree的更多相关文章

  1. [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

    既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...

  2. 二叉树系列 - 二叉搜索树 - [LeetCode] 中序遍历中利用 pre节点避免额外空间。题:Recover Binary Search Tree,Validate Binary Search Tree

    二叉搜索树是常用的概念,它的定义如下: The left subtree of a node contains only nodes with keys less than the node's ke ...

  3. LeetCode(99) Recover Binary Search Tree

    题目 Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chang ...

  4. LeetCode算法题-Trim a Binary Search Tree(Java实现)

    这是悦乐书的第284次更新,第301篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第152题(顺位题号是669).给定二叉搜索树以及L和R的最低和最高边界,修剪树以使其所 ...

  5. 【leetcode刷题笔记】Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  6. 【leetcode刷题笔记】Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  7. 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  8. 【leetcode刷题笔记】Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  9. 【leetcode刷题笔记】Word Search

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  10. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

随机推荐

  1. 【BZOJ4548】小奇的糖果 set(链表)+树状数组

    [BZOJ4548]小奇的糖果 Description 有 N 个彩色糖果在平面上.小奇想在平面上取一条水平的线段,并拾起它上方或下方的所有糖果.求出最多能够拾起多少糖果,使得获得的糖果并不包含所有的 ...

  2. z-index随笔

    z-index这个css属性是用来控制层级的,但是它的表现有点奇怪,这里做下记录. 假如有ABC三层,都没设置z-index时,是按dom节点顺序来控制层级. 此时如果B元素下有个元素D,设置了z-i ...

  3. zookeeper(二): Curator vs zkClient

    目录 zookeeper Curator zkClient 客户端对比 写在前面 1.1. zookeeper应用开发 1.1.1. ZkClient简介 1.1.2. Curator简介 写在最后 ...

  4. jquery点击一组按钮中的一个,跳转至对应页面处理策略。(如点击订单列表中的一个订单,跳转至该订单的详情)

    将改组按钮的数据设置一个相同的属性(如class),然后每个按钮设置不同的id 当用户点击属性为class的按钮,根据id属性来判断点击的是哪个按钮,然后进行相关操作. 代码示例: <scrip ...

  5. TCP协议要点和难点全解

    转载自http://www.cnblogs.com/leetieniu2014/p/5771324.html TCP协议要点和难点全解 说明: 1).本文以TCP的发展历程解析容易引起混淆,误会的方方 ...

  6. ElasticSearch(二十一)正排和倒排索引

    1.区别 搜索的时候,要依靠倒排索引:排序的时候,需要依靠正排索引,看到每个document的每个field,然后进行排序,所谓的正排索引,其实就是doc values 在建立索引的时候,一方面会建立 ...

  7. PAT 1061. 判断题(15)

    判断题的评判很简单,本题就要求你写个简单的程序帮助老师判题并统计学生们判断题的得分. 输入格式: 输入在第一行给出两个不超过100的正整数N和M,分别是学生人数和判断题数量.第二行给出M个不超过5的正 ...

  8. centos install docker setup centos7 安装docker

    centos7 安装docker 1: 安装必要的一些系统工具sudo yum install -y yum-utils device-mapper-persistent-data lvm2 2: 添 ...

  9. 如何禁止eclipse对js文件的校验(building validate)

    在项目(project)上点击右键,依次选择1.Select Properties -> JavaScript -> Include Path2.Select Source tab. ( ...

  10. 【ELK】抓取AWS-ELB日志的logstash配置文件

    前言 ELK搭建没有难度,难的是logstash的配置文件,logstash主要分为三个部分,input,filter和output. input,输入源可选的输入源由很多,详情见ELK官网,这里我们 ...