给定一个二叉树,原地将它展开为链表。
例如,给定二叉树
1
/ \
2 5
/ \ \
3 4 6
将其展开为:
1
\
2
\
3
\
4
\
5
\
6
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
void DFS(struct TreeNode* root,struct TreeNode** pre)
{
if(!root)return;
struct TreeNode*right=root->right;
if(*pre)
{
(*pre)->left=NULL;
(*pre)->right=root;
}
*pre=root;
if(root->left)DFS(root->left,pre);
if(right)DFS(right,pre);
}
void flatten(struct TreeNode* root){
struct TreeNode* pre=NULL;
DFS(root,&pre);
}
执行用时 :8 ms, 在所有C提交中击败了96.61% 的用户
内存消耗 :8.1 MB, 在所有C提交中击败了92.59%的用户