"Sword point offer" binary search tree to bidirectional linked list, C + + implementation

Source: Internet
Author: User

Original blog, reproduced please indicate the source!

# topics

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. Requires that no new nodes can be created, only the pointer to the node in the tree is adjusted.

    • Definition of binary tree node
  1/*  2 struct TreeNode {  3 int val;   4 struct TreeNode *left;   5 struct TreeNode *right;   6 TreeNode (int x):  7 val (x), left (null), right (null) {  8 }   9 }; */
    • Example of binary search tree-to-doubly linked list

# ideas

    • The nature of binary search tree
      • Binary search tree is left sub-tree < root node < right subtree
      • The middle sequence traversal of binary search tree is an ascending ordered sequence
    • Thinking of binary search tree to bidirectional chain list
      • The left pointer of the root node of the binary search tree points to the largest node of Zuozi, and the right subtree of the root node points to the smallest node of the right subtree.


# code

1 / * 2 struct TreeNode { 3 int val;  4 struct TreeNode *left; 5 struct TreeNode *right; 6 TreeNode (int x): 7 val (x), left (null), right (NULL) { 8 } 9};*/Ten classSolution { One  Public: Atreenode* Convert (treenode* prootoftree) -{/* The struct TreeNode  {int val,  TreeNode *left, and the  struct TreeNode *  Right TreeNode (int x): Val  (x), left (null), right (null) {};*/}      A classSolution { at  Public: -treenode* Convert (treenode* prootoftree) -{ -         //Bidirectional chain footer node -treenode* list_last = nullptr; -  in         //Recursive conversion -Convertnode (Prootoftree,list_last); to  +         //Doubly linked list first node -          while(List_last->left! = nullptr)//Boundary conditions the{ *List_last = list_last->left; $}Panax Notoginseng  -         //Return to the first node of the doubly linked list the         returnList_last; +} A  the     voidConvertnode (treenode* cur,treenode* list_last) +{ -         //Boundary conditions $         if(CUR==NULLPTR)return; $  -         //traverse the left subtree -         if(Cur->left! = nullptr) Convertnode (Cur->left,list_last); the  -         //Implement two-way link (establish connection)WuyiCur->left = List_last; the         if(List_last! = nullptr) list_last->right = cur; -List_last = cur; Wu  -         //Traverse Right sub-tree About         if(Cur->right! = nullptr) Convertnode (Cur->right,list_last); $} -}; -         if(Prootoftree = = NULL)returnNULL; -  ATreeNode *pointer = NULL; +  theConvert2list (prootoftree, pointer); -  $          while(Pointer->left!=null) the{ thepointer = pointer->left; the} the         returnPointer -} in     voidConvert2list (treenode* proot,treenode *&pointer) the{ the         if(Proot = = NULL) About{ the             return; the} the{ +             if(Proot->left! = NULL) -{ theConvert2list (Proot->left,pointer);Bayi} the  theProot->left = pointer; -             if(pointer! = NULL) -{ thePointer->right = Proot; the} the  thepointer = Proot; -             if(Proot->right!=null) the{ theConvert2list (proot->right, pointer); the}94} the} the};

"Sword point offer" binary search tree to bidirectional linked list, C + + implementation

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.