Understanding of "binary search tree and doubly linked list" on the Offer of swords

Source: Internet
Author: User

Title Description:

Enter a binary search tree and convert the two-fork search tree into a sorted doubly linked list. Requires that no new nodes can be created, only the point pointer of the node in the tree can be adjusted.


first, the idea of recursion

For the function treenode* convert (treenode* root), the header node of the two-fork tree that needs to be converted is passed in, and the head node of the converted doubly linked list is returned. From the root node, the idea of recursion is to make a black box for the left child and the right child, and the left = CONVERT (Root->left) returns the head node of the two-way linked list that the tree with the child as the root has transformed, right = CONVERT (root- >right) is returned to the right child as the root node of the tree has been transformed into a good two-way linked list of the head node, as long as left and right is not empty, use the leave to find the last node of the two-way link list node, will node,root,right a connection, problem solving.


steps:

1, root node is root, recursive root left child, the left sub-tree form a doubly linked list, and return to the link list header node

2. Use left head node to traverse to the last node of the double linked list

3. If the left Dial hand tree list is not empty, connect node and root

4, the same way to the right subtree to form a doubly linked list, and return to the link list head node.

5. If the right sub-tree list is not empty, connect root

6. If left is not empty, return to


The code is as follows:

/* struct TreeNode {int val;
	struct TreeNode *left;
	struct TreeNode *right; TreeNode (int x): Val (x), left (null), right (null) {}};*/class Solution {public:treenode* Convert (treenode* ro
        OT) {if (root = null) {return null;
        } if (root->left = = NULL && Root->right = = null) {return root;
        }//1, root node is root, recursive root left child, the left subtree constitutes a doubly linked list, and returns the node of the linked list of the end of the leftmost treenode* leave = Convert (root->left);
        treenode* node = left; 2. Use the left head node to traverse to the last node of the two-linked list node while (node! = NULL && node->right! = null) {node = node-&
        Gt;right;
            }//while//3, if the left Dial hand tree list is not empty, connect node and root together if (left! = NULL) {node->right = root;
        root->left = node;
        }//4, the same way the right subtree constitutes a doubly linked list, and returns the node of the linked list of the end of the first-treenode*-o = Convert (root->right); 5. If the right sub-tree list is not empty, connect the root and starboard to the if (-= = NULL) {root->right = Right;
        Right->left = root;
    }//6, if left is not empty, returns back to "return", otherwise returns root return = = Null?root:left; }
};


second, non-recursive thinking

The idea of ergodic non-recursion in binary tree sequence


Code:

struct treenode{int val;
    Treenode* left;
treenode* right;

};
    In contrast, the non-recursive middle order traversal of binary tree method void Inorder (treenode* root) {////non-recursive version of the sequential traversal if (NULL = = root) {return;
    } stack<treenode*> data;
    treenode* node = root;
            while (Node!=null | |!data.empty ()) {while (node! = NULL) {data.push (node);
        node = node->left;
            }//while if (!data.empty ()) {node = Data.top ();
            Data.pop ();
            cout<<node->val<<endl;
        node = node->right;
}}//while return;
    } treenode* Convert (treenode* root) {if (null = = root) {return null;
    } if (root->left = = NULL && Root->right = = null) {return root;
    } stack<treenode*> data;
    treenode* node = root;  treenode* pre = NULL;
    The last node bool IsFirst = True for saving the middle sequence traversal sequence; while (node! = NULL | |!data.empty ()) {while (node! = null) {Data.push (node);
        node = node->left;
        } node = Data.top ();
        Data.pop ();   if (isFirst) {root = node;
            The first node in the middle sequence traversal is recorded as root pre = root;
        IsFirst = false;
            }else{pre->right = node;
            Node->left = pre;
        Pre = node;
    } node = node->right;
} return root; }


New Ket Network title address:

https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5?tpId=13&tqId=11179&rp=2&ru=/ta/ Coding-interviews&qru=/ta/coding-interviews/question-ranking


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.