天题系列: Recover Binary Search Tree

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?

ref

http://www.cnblogs.com/springfor/p/3891390.html

  1
  /  5   3
    /
   4
         2

just like in the ref" 所以在中序便利时,遇见的第一个顺序为抵减的两个node,大的那个肯定就是要被recovery的其中之一,要记录。"

in order search 的缘故是因为这样可以保证左面能先被traverse到,就符合了找到第一个swap点的目的

public class Solution {
    TreeNode first, second, pre;
    public void recoverTree(TreeNode root) {
        if(root==null) return;
        inorder(root);
        if(first!=null && second!=null){
            int tmp=first.val;
            first.val = second.val;
            second.val = tmp;
        }
        return;
    }
    private void inorder(TreeNode node){
        if(node==null) return;
        inorder(node.left);
        if(pre==null){
            pre=node;
        }else{
            if(pre.val>node.val){
                if(first==null){
                    first=pre;
                }
                second = node; //keep moving on to find the second on the right side
            }
            pre = node;
        }
        inorder(node.right);
    }
}
时间: 2024-12-18 10:46:04

天题系列: Recover Binary Search Tree的相关文章

【leetcode刷题笔记】Recover Binary Search Tree

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? 题解:需要找到二叉搜索树中乱序的两个节点,并把它们交换回来

[leetcode]Recover Binary Search Tree @ Python

原题地址:https://oj.leetcode.com/problems/recover-binary-search-tree/ 题意: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. 解题思路:这题是说一颗二叉查找树中的某两个节点被错误的交换了,需要恢复成原来的正确的二叉查找树. 算法一:思路很简单,一颗二叉查

LeetCode: Recover Binary Search Tree

LeetCode: Recover Binary Search Tree 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 s

【leetcode】Recover Binary Search Tree

Recover Binary Search Tree 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? 中序

Recover Binary Search Tree

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? /** * Definition for binary t

LeetCode详细分析 :: Recover Binary Search Tree [Tree]

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? confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ. 这里

39. Recover Binary Search Tree && Validate Binary Search Tree

Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ 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 prett

leetcode 之 Recover Binary Search Tree

Recover Binary Search Tree 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? c

LeetCode: Recover Binary Search Tree [099]

[题目] 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? confused what "{1,#,2,3

099 Recover Binary Search Tree

099 Recover Binary Search Tree 没有按照要求用 constant space.... 用valid BST 找出两个not in order 的nodes class Solution: def recoverTree(self, root): [fN, sN] = self.valid(root) fN.val, sN.val = sN.val, fN.val def valid(self, root): pre, fN, sN = TreeNode(-10000