将二叉树拆成链表

来源:互联网 发布:淘宝客sina 编辑:程序博客网 时间:2024/06/08 08:42
样例
              1
               \
     1          2
    / \          \
   2   5    =>    3
  / \   \          \
 3   4   6          4
                     \
                      5
                       \

                        6

class Solution {  
public:  
    /**  
     * @param root: a TreeNode, the root of the binary tree  
     * @return: nothing  
     */  
    // TreeNode *x;  
    // x=new TreeNode;  
    vector<int> a;  
     void look(TreeNode *root)  
     {  
         if(root==NULL)  
         return ;  
         a.push_back(root->val);  
         look(root->left);  
         look(root->right);  
     }  
    void flatten(TreeNode *root)   
    {  
        look(root);  
        TreeNode *x;  
       x=new TreeNode;  
        x=root;  
         for(int i=0;i<a.size();i++)  
         {  
               
             x->val=a[i];  
             x->left=NULL;  
             if(i!=a.size()-1)  
             x->right=new TreeNode;  
             x=x->right;  
         }  // write your code here  
        //  x->right=NULL;  
    }  
};  

0 0
原创粉丝点击