[LeetCode] Flatten Binary Tree to Linked List

Source: Internet
Author: User

Given a binary tree, flatten it to a linked list in-place.

For example,
Given

         1        / \       2   5      / \   \     3   4   6
The flattened tree shoshould look like:
1 \ 2 \ 3 \ 4 \ 5 \ 6 Problem description: Given a binary tree, it is converted into a linked list similar to the above, and the operation should be performed in the same place. Based on the two trees above, we can see that in the form of a linked list, the left child of the node is empty, and the right child traverses the previous node of the current node in the first order. Therefore, you can construct such a function to convert the tree with the root into a chain table like above, and return the last node that traverses the entire subtree In the first order. So how does it implement this process? Call the above function on the left subtree to convert the left to the form of a linked list, and obtain the last node last_node that is first traversed in the left subtree. Then, leave the left child of the last vertex empty, the right child to root-> right, And Then root-> right = root-> left. Finally, if root-> right is not empty, call the above function on the right subtree and return the last node of the right subtree. In another special case, the left and right subtree are empty. In this case, no operation is performed and the node itself is returned.
#include 
  
   #include 
   
    #include 
    
     using namespace std;struct TreeNode {    int val;    TreeNode *left;    TreeNode *right;    TreeNode(int x) : val(x), left(NULL), right(NULL) {}};void tree_insert(TreeNode *&root, TreeNode *pnode){    if(root == NULL) {        root = pnode;        return;    }    if(pnode->val <= root->val) {        tree_insert(root->left, pnode);    }    else {        tree_insert(root->right, pnode);    }}void tree_create(TreeNode *&root, vector
     
      ::iterator beg, vector
      
       ::iterator end){    TreeNode *pnode = NULL;    while(beg != end) {        pnode = new TreeNode(*beg);        tree_insert(root, pnode);        ++beg;    }}void tree_traverse(TreeNode *root){    if(root != NULL) {        cout << root->val << " ";        tree_traverse(root->left);        tree_traverse(root->right);    }}void tree_structure(TreeNode *root){    if(root != NULL) {        cout << "root :" << root->val << endl;        if(root->left == NULL) {            cout << "left child is NULL " << endl;            tree_structure(root->right);        }        else            cout << "error!" << endl;    }}void tree_destroy(TreeNode *&root){    if(root != NULL) {        tree_destroy(root->left);        tree_destroy(root->right);        delete root;    }}class Solution {public:    TreeNode *flatten_child(TreeNode *root)    {        if(root != NULL) {            if(root->left == NULL && root->right == NULL)                return root;            TreeNode *last_node = NULL;            if(root->left == NULL) {                return flatten_child(root->right);            }            last_node = flatten_child(root->left);            if(root->right == NULL) {                root->right = root->left;                root->left = NULL;                return last_node;            }            last_node->right = root->right;            last_node->left = NULL;            root->right = root->left;            root->left = NULL;            return flatten_child(last_node->right);        }    }    void flatten(TreeNode *root)    {        if(root != NULL)            flatten_child(root);    }};int main(int argc, char *argv[]){    int arr[] = {3, 2, 1, 4, 5};    int len = sizeof(arr) / sizeof(arr[0]);    vector
       
         vec(arr, arr + len); TreeNode *tree = NULL; tree_create(tree, vec.begin(), vec.end()); tree_traverse(tree); cout << endl; Solution sol; sol.flatten(tree); tree_structure(tree); tree_destroy(tree); return 0;}
       
      
     
    
   
  

The entire program is provided to demonstrate the tree structure and changes. First, a binary tree is constructed, and then transformed. Finally, the structure of the tree is printed using tree_structure.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.