题解 | #二叉搜索树与双向链表#

二叉搜索树与双向链表

https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};*/
class Solution {
  public:
    TreeNode* Convert(TreeNode* pRootOfTree) {
        if (pRootOfTree == nullptr) {
            return {};
        }
        TreeNode* lf = pRootOfTree->left;
        TreeNode* rg = pRootOfTree->right;
        TreeNode* tp = lf;
        if (lf) {
            lf = Convert(lf);
            tp = lf;
            while (tp->right) {
                tp = tp->right;
            }
            tp->right = pRootOfTree;
            pRootOfTree->left = tp;
        }
        if (rg) {
            rg = Convert(rg);
            rg->left = pRootOfTree;
            pRootOfTree->right = rg;
        }
        return lf == nullptr ? pRootOfTree : lf;
    }
};

全部评论

相关推荐

有趣的牛油果开挂了:最近这个阶段收到些杂七杂八的短信是真的烦
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务