Binary tree transforms into doubly linked list

Source: Internet
Author: User

Given a binary tree as follows:
This topic at a glance is very simple, because this binary tree is a more formal two-fork tree, so the traversal of this tree if the use of the middle sequence traversal, then exactly the desired list of the list. But how do you chain them up? In fact, it is also very single. All we have to do is to add the original middle order traversal to the end of the list of the linked list. But this problem is still difficult for me a lot of time, ...I never thought I'd pass in a list first.Source code:
  
 
  1. #ifndef TREE_TOULIST_H
  2. #define TREE_TOULIST_H
  3. #include"reconstructBinaryTree.h"
  4. void convertNode(TreeNode *t_node,TreeNode **plastNodeList);
  5. TreeNode* treeToduTree(TreeNode **head);
  6. TreeNode* treeToduTree(TreeNode **head){
  7. if((*head)==NULL||head==NULL){
  8. return NULL;
  9. }
  10. TreeNode *root=NULL;
  11. convertNode(*head,&root);
  12. while(root!=NULL&&root->left!=NULL){
  13. root=root->left;
  14. }
  15. return root;
  16. }
  17. void convertNode(TreeNode *t_node,TreeNode **plastNodeList){
  18. if(t_node==NULL){
  19. return;
  20. }
  21. if(t_node->left!=NULL){
  22. convertNode(t_node->left,plastNodeList);
  23. }
  24. t_node->left=*plastNodeList;
  25. if(*plastNodeList!=NULL){
  26. (*plastNodeList)->right=t_node;
  27. }
  28. *plastNodeList=t_node;
  29. if(t_node->right!=NULL){
  30. convertNode(t_node->right,plastNodeList);
  31. }
  32. }
  33. #endif


 
   
  
  1. t_node->left=*plastNodeList;
  2. if(*plastNodeList!=NULL){
  3. (*plastNodeList)->right=t_node;
  4. }
  5. *plastNodeList=t_node;
The key code, you see, is nothing more than a doubly linked list is constantly added to the non-element pendulum.


From for notes (Wiz)

Binary tree transforms into doubly 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.