99. Recover Binary Search Tree

2 篇文章 0 订阅
1 篇文章 0 订阅
这道LeetCode题目要求在保持二叉搜索树结构不变的情况下,恢复因错误交换导致的两个节点。通过中序遍历找到两个错位的节点并交换,可以实现O(n)空间复杂度的解决方案。对于特殊情况,如多于两个错位节点,需注意正确处理second_element指针。
摘要由CSDN通过智能技术生成

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?

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 这道题一开始是我想复杂了,我以为是一颗置乱树的重排序问题,但这里只是两个树的错位,就简单很多,只要找到错位的两个数就行了。分别用指针指向这两个数,再将其交换就行了。
 我之前一直在烦恼如何用指针表示二叉树中两个相邻的节点,原因在于还不真正懂得递归,在这个inordertra函数里先遍历左子树便是会一直深入到整棵树最左边的节点,那个节点就是整棵树中最小的节点,pre便从最小的节点开始,当pre是空的时候,pre与_root指向同一个节点。然后深入右子树最小的节点,然后再跳到_root节点。这就可以保证pre和_root节点是相邻的两个节点。这里最重要的应该是pre指针对于inordertra函数来说是一个外部变量,而_root指针是一个内部变量,随着递归不断改变;


 */

class Solution {
public:
    void recoverTree(TreeNode* root) 
    {
        inordertra(root);
        swap(first->val,second->val);
    }
private:
    TreeNode* pre = NULL;
    TreeNode* first = NULL;
    TreeNode* second = NULL;
    void inordertra(TreeNode* _root)
    {
        if(!_root) return;
        inordertra(_root->left);
        if(pre && pre->val > _root->val)
        {
            if(!first) first = pre;
            second = _root;
        }
        pre = _root;
        inordertra(_root->right);    
    }
};

补充 : 这道题只是两个乱序的元素,所以最多只会出现① > ②  > ③ 或者 ① > ②这样的情况,才能通过互换两个元素得到正确的二叉搜索树,而对于① > ②  > ③ 这样的情况,first_element指针不改变 , 但是second_element的指针需要一直改变; 

/**
 * 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 {
    
    TreeNode *pre_element = NULL , *first_element = NULL , *second_element = NULL ;
              
public:
    void recoverTree(TreeNode* root) 
    {
        inOrder(root) ;
        swap(first_element->val , second_element->val) ;
    }
    
    void inOrder(TreeNode* root)
    {
        if(!root) return ;
        
        inOrder(root->left);
        if(pre_element && pre_element->val > root->val) 
        {
            if(!first_element) first_element = pre_element ;
            second_element = root ;
            cout<<"in"<<"pre_element.val : "<<pre_element->val<<endl;
        }
        if(root) pre_element = root ;
        cout<<"pre_element.val : "<<pre_element->val<<endl;
        inOrder(root->right) ;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值