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?

1.O(n) space solution

中序遍历该BST,得到所有整数对齐排好序,对节点重新进行赋值

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public void recoverTree(TreeNode root) {
12         // Start typing your Java solution below
13         // DO NOT write main() function
14         if(root == null){
15             return ;
16         }
17         ArrayList<TreeNode> nodes = new ArrayList<TreeNode>();
18         ArrayList<Integer> values = new ArrayList<Integer>();
19         inOrderTraverse(root, nodes, values);
20         Collections.sort(values);
21         for(int i = 0; i < nodes.size(); i++){
22             nodes.get(i).val = values.get(i);
23         }
24     }
25     
26     public void inOrderTraverse(TreeNode node, ArrayList<TreeNode> nodes, ArrayList<Integer> values){
27         if(node == null){
28             return;
29         }
30         if(node.left != null){
31             inOrderTraverse(node.left, nodes, values);
32         }
33         nodes.add(node);
34         values.add(node.val);
35         if(node.right != null){
36             inOrderTraverse(node.right, nodes, values);
37         }
38     }
39 }

 2.using two pointer, worst case O(n) space cost, average O(logN)

have some strange problem, pre is alway null!

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public void recoverTree(TreeNode root) {
12         // Start typing your Java solution below
13         // DO NOT write main() function
14         if(root == null){
15             return ;
16         }
17         TreeNode p1 = null, p2 = null, pre = null;
18         find(root, p1, p2, pre);
19         swap(p1, p2);
20     }
21     
22     public void find(TreeNode node, TreeNode p1, TreeNode p2, TreeNode pre){
23         if(node == null){
24             return;
25         }
26         find(node.left, p1, p2, pre);
27         if(pre != null && node.val < pre.val){
28             if(p1 == null){
29                 p1 = pre;
30             } else {
31                 p2 = node;
32             }
33         }
34         pre = node;
35         find(node.right, p1, p2, node);
36     }
37     
38     public void swap(TreeNode p1, TreeNode p2){
39         if(p1 != null && p2 != null){
40             int tmp = p1.val;
41             p1.val = p2.val;
42             p1.val = tmp;
43         }
44     }
45 }

3. using threaded binary tree to do the inorder traverse

 

http://tech-wonderland.net/blog/leetcode-recover-binary-search-tree.html

http://fisherlei.blogspot.com/2012/12/leetcode-recover-binary-search-tree.html

posted @ 2013-08-15 22:16  feiling  阅读(372)  评论(0编辑  收藏  举报