题解 | #二叉搜索树与双向链表#
二叉搜索树与双向链表
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* pre; void traversal(TreeNode* root) { if (root) { traversal(root->left); root->left = pre; if (pre) { pre->right = root; } pre = root; traversal(root->right); } } TreeNode* Convert(TreeNode* pRootOfTree) { if (!pRootOfTree) return nullptr; TreeNode* p = pRootOfTree; while (p->left) { p = p->left; } traversal(pRootOfTree); return p; } };