zoukankan      html  css  js  c++  java
  • Flatten Binary Tree to Linked List

    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
    

    click to show hints.

    Hints:

    If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.

    C++代码如下:

    #include<iostream>
    #include<new>
    #include<vector>
    #include<stack>
    using namespace std;
    
    //Definition for binary tree
    struct TreeNode
    {
        int val;
        TreeNode *left;
        TreeNode *right;
        TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    };
    class Solution
    {
    public:
        void flatten(TreeNode *root)
        {
            if(root==NULL)
                return;
            vector<TreeNode*> vec;
            preorder(root,vec);
            TreeNode *tmp=root;
            size_t i;
            for(i=1; i<vec.size(); i++)
            {
                //一定要记得将左指针置为NULL
                tmp->left=NULL;
                tmp->right=vec[i];
                tmp=tmp->right;
            }
            tmp->left=tmp->right=NULL;
        }
        void preorder(TreeNode *root,vector<TreeNode*> &vec)
        {
            stack<TreeNode*> s;
            while(root||!s.empty())
            {
                while(root)
                {
                    vec.push_back(root);
                    s.push(root);
                    root=root->left;
                }
                if(!s.empty())
                {
                    root=s.top();
                    s.pop();
                    root=root->right;
                }
            }
        }
        void createTree(TreeNode *&root)
        {
            int i;
            cin>>i;
            if(i!=0)
            {
                root=new TreeNode(i);
                if(root==NULL)
                    return;
                createTree(root->left);
                createTree(root->right);
            }
        }
    };
    int main()
    {
        Solution s;
        TreeNode *root;
        s.createTree(root);
        s.flatten(root);
        while(root)
        {
            cout<<root->val<<" ";
            root=root->right;
        }
    }

    运行结果:

    提交到leetcode时,一直报错,开始不太了解,后来才发现是因为左指针一直没有设为NULL.

    class Solution {
    public:
        void flatten(TreeNode *root)
        {
            if(root==NULL)
                return;
            vector<TreeNode*> vec;
            preorder(root,vec);
            TreeNode *tmp=root;
            size_t i;
            for(i=1; i<vec.size(); i++)
            {
                tmp->right=vec[i];
                tmp=tmp->right;
            }
            tmp->right=NULL;
        }
        void preorder(TreeNode *root,vector<TreeNode*> &vec)
        {
            stack<TreeNode*> s;
            while(root||!s.empty())
            {
                while(root)
                {
                    vec.push_back(root);
                    s.push(root);
                    root=root->left;
                }
                if(!s.empty())
                {
                    root=s.top();
                    s.pop();
                    root=root->right;
                }
            }
        }
        void createTree(TreeNode *&root)
        {
            int i;
            cin>>i;
            if(i!=0)
            {
                root=new TreeNode(i);
                if(root==NULL)
                    return;
                createTree(root->left);
                createTree(root->right);
            }
        }
    };

    注意,最后的指针哪些该设置为空。

  • 相关阅读:
    C++ 线程的创建、挂起、唤醒和结束 &&&& 利用waitForSingleObject 函数陷入死锁的问题解决
    接收数据界面卡顿-----待整理
    vs2012 在调试或运行的过程中不能加断点
    matlab 学习笔记
    周立功USBCAN-II 上位机开发(MFC)
    vs添加静态链接库+添加动态链接库+添加头文件目录
    Go 面试每天一篇(第 2 天)
    http 协议
    Samba配置
    svn checkout 单个文件
  • 原文地址:https://www.cnblogs.com/wuchanming/p/4108754.html
Copyright © 2011-2022 走看看