题解 | #二叉搜索树与双向链表#
二叉搜索树与双向链表
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 = nullptr; TreeNode* head = nullptr; TreeNode* Convert(TreeNode* pRootOfTree) { if (pRootOfTree == nullptr) return pRootOfTree; inorder(pRootOfTree); return head; } void inorder(TreeNode* root){ if (root == nullptr) return; inorder(root->left); if (pre != nullptr){ pre->right = root; root->left = pre; } else { //这里整个程序执行只会进入一次 head = root; } pre = root; inorder(root->right); } };