99. 恢复二叉搜索树

99. 恢复二叉搜索树

题目描述

点这里

思路分析

Morris遍历
在这里插入图片描述

代码实现

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void recoverTree(TreeNode* root) {
        TreeNode *first=NULL,*second,*last=NULL;
        while(root){
            if(!root->left){
                if(last&&last->val>root->val){
                    if(!first) first=last,second=root;
                    else second=root;
                }
                last=root;
                root=root->right;
            }
            else{
                auto p=root->left;
                while(p->right&&p->right!=root) p=p->right;
                if(!p->right) p->right=root,root=root->left;
                else{
                    p->right=NULL;
                    if(last&&last->val>root->val){
                        if(!first) first=last,second=root;
                        else second=root;
                    }
                    last=root;
                    root=root->right;
                }
            }
        }
        swap(first->val,second->val);
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我来解答你的问题。 对于这个问题,我们可以按照以下步骤来解决: 步骤1:构建二叉搜索树 我们可以使用给定的数据元素来构建一棵二叉搜索树,具体构建方法可以参考二叉搜索树的构建方法。 步骤2:遍历二叉搜索树 同样按照二叉搜索树的遍历方法,对构建好的二叉搜索树进行遍历,可选择前序遍历、中序遍历或后序遍历。 步骤3:转换为双链表 对于二叉搜索树转换为双链表,我们可以使用中序遍历,按照从小到大的顺序将树中的节点连接起来。具体实现方法如下: 1. 定义一个全局变量pre,用来记录上一个节点 2. 对于每个节点,先将其左子树转换为双链表,然后将该节点与pre连接起来,更新pre为当前节点,最后将右子树转换为双链表 3. 最后返回双链表的头节点(即最左侧的节点) 步骤4:打印双链表 按照双链表的结构,我们可以从头节点开始遍历,依次输出每个节点的值。 步骤5:按正反序输出双链表中的所有元素 对于双链表的正反序输出,我们可以定义两个指针,一个指向头节点,一个指向尾节点,然后分别从头尾开始遍历输出即可。 下面是示例代码,你可以参考一下: ```python # 定义二叉树节点 class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right # 构建二叉搜索树 def buildBST(nums): if not nums: return None mid = len(nums) // 2 root = TreeNode(nums[mid]) root.left = buildBST(nums[:mid]) root.right = buildBST(nums[mid+1:]) return root # 中序遍历将二叉搜索树转换为双链表 def convertBSTtoDLL(root): global pre pre = None def inorder(root): nonlocal pre if not root: return None inorder(root.left) if pre: pre.right = root root.left = pre pre = root inorder(root.right) inorder(root) while root.left: root = root.left return root # 打印二叉树 def printTree(root): if not root: return None print(root.val) printTree(root.left) printTree(root.right) # 打印双链表 def printDLL(head): while head: print(head.val) head = head.right # 按正序输出双链表 def printDLLForward(head): while head: print(head.val) head = head.right # 按反序输出双链表 def printDLLBackward(head): while head.right: head = head.right while head: print(head.val) head = head.left # 测试代码 nums = [1, 2, 3, 4, 5, 6, 7] root = buildBST(nums) print("打印二叉树:") printTree(root) head = convertBSTtoDLL(root) print("打印双链表:") printDLL(head) print("按正序输出双链表:") printDLLForward(head) print("按反序输出双链表:") printDLLBackward(head) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DataPlayerK

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值