Problem Link:

https://oj.leetcode.com/problems/recover-binary-search-tree/

We know that the inorder traversal of a binary search tree should be a sorted array. Therefore, we can compare each node with its previous node in the inorder to find the two swapped elements.

There are at most two cases that current_node < prev_node:

  1. If the two swapped elements are adjacent, then there is only one occurrence that current_node < prev_node. The two elements are just the current_node and prev_node.
  2. If the two swapped elements are not neighbors, then there are two occurrences that current_node < prev_node. The two elements are the prev_node in the first occurrence and the current_node in the second occurrence.

The code is as follows.

# Definition for a  binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return a tree node
def recoverTree(self, root):
"""
Traverse the tree in the order of inorder and compare the node to its previous node.
There are at most two occurrences that prev_node < current_node, if there are two elements swapped.
If there is only one occurrence, then the two swapped nodes are prev_node and current_node.
If there are two occurrences, then the two swapped nodes are prev_node in the first occurrence and current_node in the second occurrence.
"""
prev_node = None
current_node = None
stack = []
p = root
res1 = None
res2 = None
while stack or p:
if p:
stack.append(p)
p = p.left
else:
current_node = stack.pop()
# Compare current node and previous node
if prev_node:
if prev_node.val > current_node.val:
if res1:
res2 = current_node
break
else:
res1 = prev_node
res2 = current_node
prev_node = current_node
p = current_node.right
res1.val, res2.val = res2.val, res1.val
return root

【LeetCode OJ】Recover Binary Search Tree的更多相关文章

  1. 【LeetCode练习题】Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  2. 【LeetCode 99】Recover Binary Search Tree

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

  3. 【LeetCode OJ】Validate Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...

  4. LeetCode OJ 99. Recover Binary Search Tree

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

  5. LeetCode OJ:Recover Binary Search Tree(恢复二叉搜索树)

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

  6. 【LeetCode练习题】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  7. 【leetcode】Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

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

  9. 【leetcode刷题笔记】Recover Binary Search Tree

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

随机推荐

  1. Linux 常用命令集合

    1. 常用命令 ls  显示当前目录下的文件和文件夹: -ltr 按时间顺序显示文件和文件夹的详细信息,不带参数的时候 只显示文件夹和文件. vi  打开文件的内容 tar -cvf file.tar ...

  2. PHP 常用函数的解释

    1.trim() 去掉字符序列左边和右边的空格 2.stripslashes() 去掉反斜线字符 3.htmlspecialchars() 把预定义的字符 "<" (小于)和 ...

  3. PDF 补丁丁 0.5.0.2713 发布(替换字库功能修正字符宽度问题)

    新版本替换字库后,采用新字库的字符宽度.基本上可以满足一般的字库替换需求.请下载新版本测试.

  4. 框架 Onboard-引导页样式制作库

    设置背景图片或者背景movie,然后在它们之上生成数个ViewController,默认是顶部一张图片,下面是标题和详细介绍,最下面是按钮和pagegithub地址  https://github.c ...

  5. DynamicJson

    json字符串解析成Dynamic对象,开源地址http://dynamicjson.codeplex.com/,访问比较慢.使用方法摘录如下: Project Descriptiondynamic ...

  6. 山锅(samgor)的博客 2014

    搞移动端的前端开发已经3年,悄悄地从一个前端小白变成老油条. 项目已经做了好几个,最近发现技术的提升貌似停滞不前.说很菜吧,知道的还是不少.说精通吧,自我感觉还算不上. 认真的考虑下这个问题,觉得技术 ...

  7. 通过select的value值来改变textarea内字体和大小

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. RSA加密,应用授权及MSSQL备份与还原

    01.QBRSA加解密处理 --> a.利用 RSA密钥生成器生成密钥(e,n,d) [e,n]为私钥, [d,n]为公钥 b.正向加密:  用私钥加密,用公钥解密 c.反向加密:  用公钥加密 ...

  9. jquery选择相同ID

    jQuery中$("#id")只能选择第一个对象,不能选择所有相同id的元素.   通过 $("input[id='xxxx']"); 可以选择多个相同id的元 ...

  10. Jmail组件-----发送email

    jmail是一个第三方邮件操作组件,通常位于web服务器端,和站点程序紧密配合来接收及提交邮件到邮件服务器的控件,让网站拥有发送邮件既接收邮件的功能. 之所以想尝试它的理由呢 是因为----jmail ...