Write a program to convert binary tree in to doubly linked list such that doubly linked list represents the spiral order of given tree using recursion, Inplace(no Extra Space except recursion memory stack)
given pointer to head of binary tree
1
/ \
2 3
/ \ / \
4 5 6 7
doubly linked list represent
head-> 1 2 3 7 6 5 4
最后的两个算法都是有错误的,以现在的两个算法为准:
如果使用额外空间,倒第二个算法,也是错误的,放入vec的顺序和从vec读取的顺序的周期有问题,不能螺旋打印。可以这样做,在连接每一层之前,先从左到右把节点的left和right放入vec中,然后在根据flag是从左到右还是从右到左把节点从vec中取出并用双链表连接,每一层flag取反:
struct node{
int val;
node *left;
node *right;
};
void append(node* &head, vector<node*>&vec, int beg, int end, bool flag ) {
if (flag) {
for (int i = beg; i <= end; ++i) {
node* n = vec[i];
n->right = NULL;
head->right = n;
n->left = head;
head = n;
}
}
else {
for (int i = end; i >= beg; --i) {
node* n = vec[i];
n->right = NULL;
head->right = n;
n->left = head;
head = n;
}
}
}
void push(vector<node*>& vec, int beg, int end) {
for (int i = beg; i <= end; ++i) {
node *n = vec[i];
if (n->left) vec.push_back(n->left);
if (n->right) vec.push_back(n->right);
}
}
node* bitree2dlink(node *root) {
if (!root) return NULL;
vector<node*> vec;
int beg = 0;
int end = 0;
vec.push_back(root);
node head;
head->left = NULL;
head->right = NULL;
node *pt = &head;
bool flag = false;
while(vec.size() > 0) {
push(vec, beg, end);
append(pt, beg, end, flag);
flag = !flag;
beg = end + 1;
end = vec.size() - 1;
}
return pt->right;
}
int height(node* root) {
if (root == NULL) return 0;
return max(height(root->left)+1, height(root->right) + 1);
}
void tree2link(node*pt, node* n, int level, bool flag) {
if (n == NULL) return;
if (level == 0) {
n->right = pt->right;
if (pt->right) pt->right->left = n;
pt->right = n;
n->left = pt;
}
if (level > 0) {
if (flag) {
tree2link(pt, n->left, level - 1, flag);
tree2link(pt, n->right, level - 1, flag);
}
else {
tree2link(pt, n->right, level - 1, flag);
tree2link(pt, n->left, level - 1, flag);
}
}
}
node *recurBitree2dl(node *root) {
if (!root) return NULL;
node head;
node* pt = &head;
int h = height(root) - 1;
bool flag = true;
for (int i = h; i >= 0; --i) {
tree2link(pt, root, i, flag);
flag = ~flag;
}
}
////////////////////////////////////////////////////////////////////////////////////
以下的代码都有错误
如果允许用额外空间,题目相对比较简单
Node *tree2Link(Node *root) {
if (root == NULL)
return NULL;
vector<Node *> vec;
bool flag = true;
vec.push_back(root);
int beg = 0, end = 0;
Node head;
Node *t = &head;
while (!vec.empty()) {
if (flag) {
int i = beg;
for (;i< = end; ++i) {
Node *tmp = vec[i];
if (tmp->left)
vec.push_back(tmp->left);
if (tmp->right)
vec.push_back(tmp->right);
t->right = tmp;
tmp->left = t;
t = tmp;
}
}
else {
int i = end;
for (; i >= beg; --i) {
Node *tmp = vec[i];
if (tmp->right)
vec.push_back(tmp->right);
if (tmp->left)
vec.push_back(tmp->left);
t->right = tmp;
tmp->left = t;
t = tmp;
}
}
beg = end + 1;
end = vec.size() - 1;
flag = ~flag;
}
return head.right;
}
不用额外空间,在网上看到一种方法,复杂度比较高,但是思路不错。
先计算出数的高度,从最低层开始逐层连接。
struct node
{
struct node *left;
struct node *right;
int data;
};
struct node *rslt;//Global structure pointer..it point to head of doubly linked list
int height(struct node* head)
{
if(head==NULL)
return 0;
if(head->left==NULL && head->right==NULL)
return 0;
int lh=height(head->left);
int rh=height(head->right);
return lh>rh?(lh+1):(rh+1);
}
struct node* appnd(struct node *a,struct node *b)
{
if(a==NULL)return b;
if(b==NULL)return a;
struct node* result=a;
while(a->left!=NULL)
a=a->left;
a->left=b;
b->right=a;
//b->left=NULL;
return result;
}
void printGivenLevel(struct node* head,int level,int ltr)
{
if(head==NULL)
return;
if(level==0)
{
appnd(rslt, head);
rslt = head;
}
if(level>0)
{
if(ltr)
{
printGivenLevel(head->left,level-1,ltr);
printGivenLevel(head->right,level-1,ltr);
}
else
{
printGivenLevel(head->right,level-1,ltr);
printGivenLevel(head->left,level-1,ltr);
}
}
}
void printGivenOrder(struct node* head)
{
int i=0;
int ltr=0;
for(i=height(head); i >= 0; i--)
{
printGivenLevel(head,i,ltr);
ltr=~ltr;
}
}
struct node* NewNode(int data)
{
struct node* tmp=(struct node *)malloc(sizeof(struct node));
tmp->data=data;
tmp->left=NULL;
tmp->right=NULL;
return tmp;
}
void printList(struct node* node)
{
struct node* current=node;
while(current)
{
printf("%d ----> ",current->data);
current=current->right;
}
}
int main()
{
struct node* start=NULL;
start=NewNode(1);
start->left=NewNode(2);
start->right=NewNode(3);
start->left->left=NewNode(4);
start->left->right=NewNode(5);
start->right->left=NewNode(6);
start->right->right=NewNode(7);
printGivenOrder(start);
printList(rslt);
return 0;
}