<span style="font-size:14px;">/*************************************************************************
> File Name: Flatten_Binary_Tree_to_Linked_List.cpp
> Author:
> Mail:
> Created Time: 2014年08月24日 星期日 15时59分18秒
************************************************************************/
/**********************************************************************
Description:
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
**********************************************************************/
#include<iostream>
#include<stdio.h>
#include<stack>
using namespace std;
struct node
{
public:
node()
{
left = NULL;
right = NULL;
mid = NULL;
}
int data;
struct node *left;
struct node *mid;
struct node *right;
};
typedef struct node * Node;
Node root;
void list_create()
{
root = new struct node;
root->data = 1;
Node node1 = new struct node;
node1->data = 2;
root->left = node1;
Node node2 = new struct node;
node2->data = 5;
root->right = node2;
Node node3 = new struct node;
node3->data = 3;
node1->left = node3;
Node node4 = new struct node;
node4->data = 4;
node1->right = node4;
Node node5 = new struct node;
node5->data = 6;
node2->right = node5;
}
stack<Node>Stack;
void Solution( Node root)
{
if(root == NULL)
{
return;
}
Node tmp = root;
Node pre = root;
while(!Stack.empty() || tmp != NULL)
{
while(tmp!= NULL)
{
Stack.push(tmp);
printf("%d\n",tmp->data);
if(tmp->left != NULL)
{
pre->mid = tmp->left;
pre = tmp->left;
}
tmp = tmp->left;
}
if(!Stack.empty())
{
tmp = Stack.top();
if(tmp->right != NULL)
{
pre->mid = tmp->right;
pre = tmp->right;
}
Stack.pop();
tmp = tmp->right;
}
}
}
int main()
{
list_create();
Solution(root);
printf("___\n");
while(root)
{
printf("%d\n",root->data);
root = root->mid;
}
}</span>
Longest_Palindromic_Substring
最新推荐文章于 2020-03-19 10:17:30 发布