AcWing 49. 二叉搜索树与双向链表
原题链接
中等
作者:
Minuet
,
2021-05-27 12:19:51
,
所有人可见
,
阅读 234
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* pre=NULL;
TreeNode* head=NULL;
TreeNode* convert(TreeNode* root) {
if(!root) return root;
dfs(root);
return head;
}
void dfs(TreeNode* root){
if(!root) return;
dfs(root->left);
if(pre==NULL) head=root;
else pre->right=root;
root->left=pre;
pre=root;
dfs(root->right);
}
};