美文网首页
49. 二叉搜索树与双向链表

49. 二叉搜索树与双向链表

作者: 蜜糖_7474 | 来源:发表于2019-10-16 11:16 被阅读0次

题目地址:https://www.acwing.com/problem/content/87/

AC代码

/**
 * 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:
    TreeNode* last=nullptr;
    
    void inOrder(TreeNode* root){
        if(!root) return;
        inOrder(root->left);
        root->left=last;
        if(last) last->right=root;
        last=root;
        inOrder(root->right);
    }

    TreeNode* convert(TreeNode* root) {
        inOrder(root);
        while(root&&root->left) root=root->left;
        return root;
    }
};

总结

题解参考讨论区

相关文章

网友评论

      本文标题:49. 二叉搜索树与双向链表

      本文链接:https://www.haomeiwen.com/subject/mamymctx.html