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:
#ifndef TREE_TOULIST_H
#define TREE_TOULIST_H
#include"reconstructBinaryTree.h"
void convertNode(TreeNode *t_node,TreeNode **plastNodeList);
TreeNode* treeToduTree(TreeNode **head);
TreeNode* treeToduTree(TreeNode **head){
if((*head)==NULL||head==NULL){
return NULL;
}
TreeNode *root=NULL;
convertNode(*head,&root);
while(root!=NULL&&root->left!=NULL){
root=root->left;
}
return root;
}
void convertNode(TreeNode *t_node,TreeNode **plastNodeList){
if(t_node==NULL){
return;
}
if(t_node->left!=NULL){
convertNode(t_node->left,plastNodeList);
}
t_node->left=*plastNodeList;
if(*plastNodeList!=NULL){
(*plastNodeList)->right=t_node;
}
*plastNodeList=t_node;
if(t_node->right!=NULL){
convertNode(t_node->right,plastNodeList);
}
}
#endif
t_node->left=*plastNodeList;
if(*plastNodeList!=NULL){
(*plastNodeList)->right=t_node;
}
*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