码迷,mamicode.com
首页 > 其他好文 > 详细

99. Recover Binary Search Tree

时间:2018-12-09 20:13:34      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:modify   instead   ant   ons   ack   void   put   python   iter   

Two elements of a binary search tree (BST) are swapped by mistake.

Recover the tree without changing its structure.

Example 1:

Input: [1,3,null,null,2]

   1
  /
 3
     2

Output: [3,1,null,null,2]

   3
  /
 1
     2

Example 2:

Input: [3,1,4,null,null,2]

  3
 / 1   4
   /
  2

Output: [2,1,4,null,null,3]

  2
 / 1   4
   /
  3

Follow up:

  • A solution using O(n) space is pretty straight forward.
  • Could you devise a constant space solution?
 

Approach #1: C++.[Recursive]

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void recoverTree(TreeNode* root) {
        if (root == NULL) return;
        helper(root);
        int temp = first->val;
        first->val = second->val;
        second->val = temp;
    }
private:
    TreeNode* first = NULL;
    TreeNode* second = NULL;
    TreeNode* prev = NULL;
    
    void helper(TreeNode* root) {
        if (root == NULL) return;
        helper(root->left);
        if (prev != NULL && prev->val > root->val) {
            if (first == NULL) first = prev;
            second = root;
        }
        prev = root;
        helper(root->right);
    }

};

  

Approach #2: Java.[Iterator]

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public void recoverTree(TreeNode root) {
        if (root == null) return;
        TreeNode first = null;
        TreeNode second = null;
        TreeNode prev = null;
        
        Stack<TreeNode> stack = new Stack<>();
        TreeNode curr = root;
        
        while (!stack.isEmpty() || curr != null) {
            if (curr != null) {
                stack.push(curr);
                curr = curr.left;
            } else {
                curr = stack.pop();
                if (prev != null && prev.val > curr.val) {
                    if (first == null) first = prev;
                    second = curr;
                }
                prev = curr;
                curr = curr.right;
            }
        }
        
        int temp = first.val;
        first.val = second.val;
        second.val = temp;
    }
}

  

Approach #3: Python. [Recursive]

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
        
    def recoverTree(self, root):
        """
        :type root: TreeNode
        :rtype: void Do not return anything, modify root in-place instead.
        """
        self.first = None
        self.second = None
        self.prev = None
        
        if root == None:
            return;
        
        self.helper(root);
        
        temp = self.first.val
        self.first.val = self.second.val
        self.second.val = temp
        
        
    def helper(self, root):
        if root == None:
            return;
        self.helper(root.left)
        
        if self.prev != None and self.prev.val > root.val:
            if self.first == None:
                self.first = self.prev
            self.second = root
        
        self.prev = root
        
        self.helper(root.right)

  

 

99. Recover Binary Search Tree

标签:modify   instead   ant   ons   ack   void   put   python   iter   

原文地址:https://www.cnblogs.com/ruruozhenhao/p/10092385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!