1. 题目描述
给定一个二叉树,原地将它展开为链表。
例如,给定二叉树
1
/
2 5
/ \
3 4 6
将其展开为:
1
2
3
4
5
6
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2.代码如下
//思路就是保留先序遍历得到的访问节点的顺序,然后按照这个序列构建链表就行。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
void flatten(struct TreeNode* root) {
//先序遍历这棵树,并构建链表
typedef struct TreeNode node;
int cur = 0;
int size = 0;
int i = 0;
node **treestack = (node**)malloc(sizeof(node*)*(cur+1));
node **rootarray = (node**)malloc(sizeof(node*)*(size+1));
node *listhead = NULL;
node *p = root;
while(cur > 0|| p)
{
if(p)
{
treestack = (node**)realloc(treestack,sizeof(node*)*(cur+1));
rootarray = (node**)realloc(rootarray,sizeof(node*)*(size+1));
rootarray[size++] = p;
treestack[cur++] = p;
p = p->left;
}
else
{
p = treestack[cur-1];
cur--;
p = p->right;
}
}
while(i < size-1)
{
listhead = rootarray[i];
listhead->left = NULL;
listhead->right = rootarray[i+1];
i++;
}
return rootarray[0];
}