99. Recover Binary Search Tree

本文介绍了一种简单的方法来解决二叉搜索树中两个节点被错误交换的问题。通过一次简单的中序遍历即可找到并修复这两个节点,无需复杂的Morris遍历。文中详细解释了如何在遍历过程中定位错误节点,并提供了具体的实现代码。

题目描述

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

Recover the tree without changing its structure.
在这里插入图片描述在这里插入图片描述在这里插入图片描述

题目链接

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

方法思路

This question appeared difficult to me but it is really just a simple in-order traversal! I got really frustrated when other people are showing off Morris Traversal which is totally not necessary here.

Let’s start by writing the in order traversal:

private void traverse (TreeNode root) {
   if (root == null)
      return;
   traverse(root.left);
   // Do some business
   traverse(root.right);
}

So when we need to print the node values in order, we insert System.out.println(root.val) in the place of “Do some business”.

What is the business we are doing here?
We need to find the first and second elements that are not in order right?

How do we find these two elements? For example, we have the following tree that is printed as in order traversal:

6, 3, 4, 5, 2

We compare each node with its next one and we can find out that 6 is the first element to swap because 6 > 3 and 2 is the second element to swap because 2 < 5.

Really, what we are comparing is the current node and its previous node in the “in order traversal”.

Let us define three variables, firstElement, secondElement, and prevElement. Now we just need to build the “do some business” logic as finding the two elements. See the code below:

public class Solution {
    //Runtime: 2 ms, faster than 98.93%
    //Memory Usage: 45.2 MB, less than 90.35%
    TreeNode firstElement = null;
    TreeNode secondElement = null;
    // The reason for this initialization is to avoid null pointer exception in the first comparison when prevElement has not been initialized
    TreeNode prevElement = new TreeNode(Integer.MIN_VALUE);
    
    public void recoverTree(TreeNode root) {
        
        // In order traversal to find the two elements
        traverse(root);
        
        // Swap the values of the two nodes
        int temp = firstElement.val;
        firstElement.val = secondElement.val;
        secondElement.val = temp;
    }
    
    private void traverse(TreeNode root) {
        
        if (root == null)
            return;
            
        traverse(root.left);
        
        // Start of "do some business", 
        // If first element has not been found, assign it to prevElement (refer to 6 in the example above)
        if (firstElement == null && prevElement.val >= root.val) {
            firstElement = prevElement;
        }
    
        // If first element is found, assign the second element to the root (refer to 2 in the example above)
        if (firstElement != null && prevElement.val >= root.val) {
            secondElement = root;
        }        
        prevElement = root;

        // End of "do some business"

        traverse(root.right);
    }
}
ECDSA.recover is a function in the ECDSA (Elliptic Curve Digital Signature Algorithm) cryptographic system that allows a user to recover the public key from a given signature and message. This function is useful in situations where the public key is unknown but the signature and message are available. The ECDSA algorithm involves three steps: key generation, signature generation, and signature verification. In the key generation step, a private key is generated using a random number generator, and the corresponding public key is derived from the private key. In the signature generation step, a message is hashed and signed using the private key to generate a signature. In the signature verification step, the signature is verified using the public key to ensure that it was generated by the owner of the private key. In some cases, the public key may not be available, but the signature and message are known. In such cases, the ECDSA.recover function can be used to recover the public key from the signature and message. The function takes three inputs: the message, the signature, and the recovery parameter. The recovery parameter is a number between 0 and 3 that specifies which of the four possible public keys should be recovered from the signature. Once the public key is recovered, it can be used to verify the signature and authenticate the message. Overall, ECDSA.recover is a useful function in the ECDSA cryptographic system that allows for public key recovery in situations where it is unknown but the signature and message are available.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值