Recover Binary Search Tree

Source: Internet
Author: User

Recover Binary Search Tree

Problem:

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?

Confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

Ideas:

Transformation of the middle sequence traversal

My Code:

 Public classSolution { Public voidrecovertree (TreeNode root) {if(Root = =NULL)return; List<TreeNode> list =NewArraylist<treenode>();        Inordertree (root, list); TreeNode Left=NULL; TreeNode Right=NULL;  for(inti = 0; I < List.size ()-1; i++) {TreeNode one=List.get (i); TreeNode= List.get (i+1); if(One.val >two.val) {if(left = =NULL) { left=One ; Right=both ; }                Else{ Right=both ; }            }        }        if(Left! =NULL&& Right! =NULL)        {            intTMP =Left.val; Left.val=Right.val; Right.val=tmp; }    }     Public voidInordertree (TreeNode root, list<treenode>list) {        if(Root = =NULL)return; if(Root.left = =NULL&& Root.right = =NULL) {list.add (root); return;        } inordertree (Root.left, list);        List.add (root);    Inordertree (root.right, list); }}
View Code

Others code 1:

 Public classRecovertree {TreeNode pre=NULL; TreeNode First=NULL; TreeNode Second=NULL;  Public voidrecovertree (TreeNode root) {inorder (root); //Swap the value of first and second node.        intTMP =First.val; First.val=Second.val; Second.val=tmp; }         Public voidinorder (TreeNode root) {if(Root = =NULL) {            return; }                //inorder Traverse.inorder (Root.left); //determine if the pre has been set        if(Pre! =NULL&& pre.val >root.val) {if(First = =NULL) {                //first found in reverse order.First =Pre; Second=Root; } Else {                //the second time to find the reverse order, update second.Second =Root; }} Pre=Root; //inorder Traverse.inorder (root.right); }
View Code

Others code 2:

 Public voidrecoverTree1 (TreeNode root) {if(Root = =NULL) {            return; } TreeNode Node1=NULL; TreeNode Node2=NULL; TreeNode Pre=NULL; Stack<TreeNode> s =NewStack<treenode>(); TreeNode cur=Root;  while(true) {             while(cur! =NULL) {s.push (cur); Cur=Cur.left; }                        if(S.isempty ()) { Break; } TreeNode Node=S.pop (); if(Pre! =NULL) {                //Invalid order                if(Pre.val >node.val) {if(Node1 = =NULL) {Node1=Pre; Node2=node; } Else{Node2=node; }}} Pre=node; Cur=Node.right; }                intTMP =Node1.val; Node1.val=Node2.val; Node2.val=tmp; return; }
View Code

The Learning Place:

    • My code has a spatial complexity of O (n)
    • Others code 1 Space Complexity of O (LGN) Others code space complexity of O (LGN), you need to note that others code 2 is the middle sequence traversal of the non-recursive algorithm, need to note that before judging several times before AC.

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.