# 二叉树展开为链表

给你二叉树的根结点 root ,请你将它展开为一个单链表:

 

示例 1:

输入:root = [1,2,5,3,4,null,6]
输出:[1,null,2,null,3,null,4,null,5,null,6]

示例 2:

输入:root = []
输出:[]

示例 3:

输入:root = [0]
输出:[0]

 

提示:

 

进阶:你可以使用原地算法(O(1) 额外空间)展开这棵树吗?

## template ```cpp #include using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; class Solution { public: void rconnect(TreeNode *&node, TreeNode *pmove) { if (pmove == nullptr) return; node->right = new TreeNode(pmove->val); node->left = nullptr; node = node->right; rconnect(node, pmove->left); rconnect(node, pmove->right); } void flatten(TreeNode *root) { if (root == nullptr) return; TreeNode *head = new TreeNode(); TreeNode *newroot = head; rconnect(head, root); newroot = newroot->right->right; root->right = newroot; root->left = nullptr; } }; ``` ## 答案 ```cpp ``` ## 选项 ### A ```cpp ``` ### B ```cpp ``` ### C ```cpp ```