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. Vue实现组件props双向绑定解决方案

    注意: 子组件不能直接修改prop过来的数据,会报错 方案一: 用data对象中创建一个props属性的副本 watch props属性 赋予data副本 来同步组件外对props的修改 watch ...

  2. 【BZOJ2407/4398】探险/福慧双修 最短路建模

    [BZOJ2407]探险 Description 探险家小T好高兴!X国要举办一次溶洞探险比赛,获奖者将得到丰厚奖品哦!小T虽然对奖品不感兴趣,但是这个大振名声的机会当然不能错过! 比赛即将开始,工作 ...

  3. Java内部类{[普通内部类][静态内部类]}

    package Learn.com.seven; /** * * @author tqw * 本例主要学习内部类的使用 * Java的内部类分成两部分来讲: * 1:内部类 * 2:静态内部类 * * ...

  4. Hive高级

    HiveServer2 概述: https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Overview2 客户端: https:// ...

  5. Bootstrap第2天

    Bootstrap第2天 解决IE低版本不支持HTML5元素的方法 CSS全局样式--栅格系统 1.栅格系统介绍     Bootstrap提供了一套响应式.移动设备优先的流式的栅格系统.     B ...

  6. 【HTTP】HTPP学习笔记

    1.了解web及网络基础 HTTP的诞生 TCP/IP协议族 应用层 FTP文件传输协议 HTTP超文本传输协议 DNS域名系统:IP地址<--->域名 传输层 TCP传输控制协议 三次握 ...

  7. C#实例,熟练使用泛型数组等,课程选择小软件

    CourseItem.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  8. spring 注解事务

    前提:在applicationContext.xml中配置<tx:annotation-driven transaction-manager="transactionManager&q ...

  9. 3.06课·········C#语言基础

    Main函数: static void Main(string [] args){ }程序代码需要写在Main函数的花括号内. 一.输出:Console.WriteLine("这是我的第一个 ...

  10. Python——轻量级web服务器flask的学习

    前言: 根据工程需要,开始上手另一个python服务器---flask,flask是一个轻量级的python服务器,简单易用.将我的学习过程记录下来,有新的知识会及时补充. 记录只为更好的分享~ 正文 ...