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

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);
        }
    }
};

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

相关文章
|
Java
Leetcode 114. Flatten Binary Tree to Linked List
思路也很简单,先把root的左子树(如有)变成单链表 leftlinkedlist,把root的右子树(如有)变成单链表 rightlinkedlist,再把root的右节点变成leftlikedlist,再把rightlinkedlist接到leftlinkedlist后面,代码如下。
49 1
|
6月前
Qt控件(按钮、单选、复选、list、tree、table)
Qt控件(按钮、单选、复选、list、tree、table)
|
7月前
|
存储 Python
链表(Linked List)详解
链表(Linked List)详解
60 0
|
JSON JavaScript 数据格式
Elementui Tree 树形控件,将勾选选中的值放在list集合里面提交
Elementui Tree 树形控件,将勾选选中的值放在list集合里面提交
81 1
list转tree,并支持搜索
list转tree,并支持搜索
69 0
|
存储 前端开发 数据库
飘乙己:List转Tree有4种写法!
飘乙己:List转Tree有4种写法!
113 1
|
C++
【PAT甲级 - C++题解】1133 Splitting A Linked List
【PAT甲级 - C++题解】1133 Splitting A Linked List
87 0
|
C++
【PAT甲级 - C++题解】1074 Reversing Linked List
【PAT甲级 - C++题解】1074 Reversing Linked List
77 0
|
机器学习/深度学习 存储 C++
【PAT甲级 - C++题解】1052 Linked List Sorting
【PAT甲级 - C++题解】1052 Linked List Sorting
88 0
|
6月前
|
安全 Java
java线程之List集合并发安全问题及解决方案
java线程之List集合并发安全问题及解决方案
1018 1