zoukankan      html  css  js  c++  java
  • 99. Recover Binary Search Tree

    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)
    

      

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    Linux下安装LoadRunner LoadGenerator
    Pytorch中的model.named_parameters()和model.parameters()
    Pytorch取最小或最大的张量索引
    Pytorch之permute函数
    softmax上溢和下溢
    Explainable ML
    ML对抗攻击
    Typora快捷键记录
    ubuntu查看目录大小
    在服务器之间传输文件
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10092385.html
Copyright © 2011-2022 走看看