题解 | #二叉搜索树与双向链表#
二叉搜索树与双向链表
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: void past_order(TreeNode* cur) { if (!cur || (!cur->left && !cur->right) ) return; past_order(cur->left); past_order(cur->right); TreeNode* temp1 = cur->left; if (temp1 != nullptr) { while (temp1->right) temp1 = temp1->right; cur->left = temp1, temp1->right = cur; } temp1 = cur->right; if (temp1 != nullptr) { while (temp1->left) temp1 = temp1->left; cur->right = temp1, temp1->left = cur; } } TreeNode* Convert(TreeNode* root) { if (!root) return root; TreeNode* res = root; while (res->left) res = res->left; past_order(root); return res; } };