题解 | #二叉搜索树与双向链表#
二叉搜索树与双向链表
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; } };