Recover Binary Search Tree

Source: Internet
Author: User

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

Elements of a binary search tree (BST) is 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?

Problem Solving Ideas:

The difficulty of this problem first is, how to determine the location of a node is wrong?

Consider the definition of BST, where all nodes of Zuozi are <=root values, and all nodes of the right subtree >root values, and so are all subtrees. This is a recursive definition. Therefore, it is not possible to DFS, only to determine whether the current node is not smaller than the left node, and smaller than the right node. Such a judgment is invalid.

Recalling Validate Binary Search Tree This problem, we can find that an important property of BST--bst inorder traversal results, is monotonically increasing. Then if a BST has two nodes exchanging positions, the results must be wrong. But how many mistakes do you have?

In the first case, 0,1,2 such a BST becomes 1,0,2, a mistake.

The second case, 0,1,2,3,4,5,6, becomes 0,5,2,3,4,1,6. Two errors.

So, the idea is: inorder traversal BST, encountered the current node Val is smaller than the previous node, indicating an error. In the first case, the current node and the previous node can be changed. In the second case, the first error places the previous node, and the second error places the current node exchange on it.

Then we can encounter the first error, the previous node and the current node are added to the list. The error follows, as long as the current node to replace the second position is possible.

The code is as follows:

/*** Definition for Binary tree * public class TreeNode {* Int. val; * TreeNode left; * TreeNode right; * TreeNode (int x) {val = x;} }*/ Public classSolution { Public voidrecovertree (TreeNode root) {List<TreeNode> list =NewArraylist<treenode>(); List<TreeNode> prelist =NewArraylist<treenode>();        DFS (root, list, prelist); TreeNode Node1= List.get (0); TreeNode Node2= List.get (1); inttemp =Node1.val; Node1.val=Node2.val; Node2.val=temp; }         Public voidDFS (TreeNode root, list<treenode> List, list<treenode>prelist) {        if(Root = =NULL) {            return;        } dfs (Root.left, list, prelist); if(prelist.size () = = 0) {prelist.add (root); } Else {            if(Root.val < Prelist.get (0). val) {if(list.size () = = 0) {List.add (Prelist.get (0));                List.add (root); } Else{List.set (1, Root); //indicates that two nodes have been found, do not pass it down again                    return; }} prelist.set (0, Root);    } dfs (Root.right, list, prelist); }}

This problem I did not make out, look at the result of the great God.

http://blog.csdn.net/linhuanmars/article/details/24566995

Recover Binary Search Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.