[C + +] leetcode:102 Flatten binary tree to Linked list (binary trees to pre-order linked list)

Source: Internet
Author: User

Topic:

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.

Answer 1: Recursive method

train of thought: observe the process of the topic conversion, we follow the sequence of sequential traversal into a linked list, but the list is still the structure of the tree, is to go straight to the right (No child) to simulate the linked list. In order to ensure the link between nodes, we maintain the pre-order traversal of the previous node, and then each time the left node of the pre is empty, the right node is set to the current nodes (that is, according to the Order of sequential traversal). Here we need to pay attention to, also need to maintain the right child node savedright, so as to facilitate the recursion. Otherwise, it is possible that the right side of the current node may be overwritten, and cannot be taken back.

The recursive method of the tree, we mainly consider the recursive end conditions and recursive conditions, if the recursive structure of the tree will be modified, we need to save the node.

Attention:

1. Maintenance of two nodes, a first-order traversal of the previous node pre, a current node of the right child node savedright. Easy to link and protect right child nodes.

Private:    treenode* pre = NULL;   To maintain a node that was last accessed, you need to connect this node to the pre
treenode* savedright = root->right;
2. Note the link process, we are the left child of the pre is empty, the right child is set as the current node, the realization of the pre-order Chain table connection.

if (pre! = null)        {            pre->left = null;//Last node left subtree is set to null            pre->right = root;//Last Traversal node right node is root        }

3. Note the sequence of pre-order traversal, first root->left recursion, followed by the maintenance of the right node recursion savedright. Always maintain the pre.

Pre = root; Flatten (Root->left); Flatten (savedright); Change the structure of the tree, continue traversing from the root right node of the saved tree
Complexity: O (N)
AC Code:

/** * Definition for binary tree * struct TreeNode {*     int val; *     TreeNode *left; *     TreeNode *right; *     Tre Enode (int x): Val (x), left (null), right (NULL) {} *}; */class Solution {public:    void Flatten (TreeNode *root) {        if (root = NULL) return;        treenode* savedright = root->right;                if (pre! = null)        {            pre->left = null;//Last node left subtree is set to null            pre->right = root;//Last Traversal node right node is root        }        Pre = root;        Flatten (root->left);        Flatten (savedright); Change the structure of the tree, continue traversing}private from the root right node of the saved tree    :    treenode* pre = NULL;   To maintain a node that was last accessed, you need to connect this node to the pre};

Answer 2: Non-recursive method

Ideas:

[Ideas for solving problems]

         1          \
2
/ \
3 4
5 6

The left subtree of the root is processed and the right subtree of the left subtree is inserted into the right subtree by the root node and the left sub-tree. Next, the node 2 is processed, and the same 2 Zuozi is inserted into the right subtree. We access the Node 2 (the left child of the root node) at the right node (the leftmost node at the top of the left subtree), and finally be accessed, and get the last nodes of the left subtree (node 4), inserted into the root junction of the right Child (node 5) of the previous nodes (nodes 4). This insertion process is ongoing.

Attention:

1. Locate the previous node of the right subtree, which is the right junction of the left child of the root node.

treenode* ptr = root->left;while (ptr->right) ptr = ptr->right;
2. Note how the procedure is inserted. Associative graph memory

Ptr->right = Root->right;root->right = Root->left;root->left = NULL;
3. Root-rooted right child, continue the insertion process, knowing that root is null, that is, Root traverses to the last node of the tree.

AC Code:

/** * Definition for binary tree * struct TreeNode {*     int val; *     TreeNode *left; *     TreeNode *right; *     Tre Enode (int x): Val (x), left (null), right (NULL) {} *}; */class Solution {public:    void Flatten (TreeNode *root) {while        (root)        {            if (root->left)            {                treenode* ptr = root->left;                while (ptr->right) ptr = ptr->right;                Ptr->right = root->right;                Root->right = root->left;                Root->left = NULL;            }            root = root->right;        }}    ;




[C + +] leetcode:102 Flatten binary tree to Linked list (binary trees to pre-order linked list)

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.