Longest_Palindromic_Substring

<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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值