1、中序遍历,当前结点,以及左侧排好序的双向链表,再调整当前结点的指针指向最前结点
/*
struct TreeNode {int val;struct TreeNode *left;struct TreeNode *right;TreeNode(int x) :val(x), left(NULL), right(NULL) {}
};*/
class Solution {
public:void Convert1(TreeNode* pRootOfTree, TreeNode* &leftsortlist){if(!pRootOfTree)//一定是引用,引用改变原来的,指针是一份实参拷贝return;TreeNode *curr=pRootOfTree;if(curr->left!=NULL)Convert1(curr->left,leftsortlist);curr->left=leftsortlist; //当前结点和左侧已经排好序的双向链表if(leftsortlist!=NULL)leftsortlist->right=curr;leftsortlist=curr;if(leftsortlist->right!=NULL)Convert1(curr->right,leftsortlist);}TreeNode* Convert(TreeNode* pRootOfTree){TreeNode *leftsortlist=NULL;Convert1(pRootOfTree,leftsortlist); //当前结点和左侧已经排好序的双向链表TreeNode *headnode=leftsortlist;while(leftsortlist && leftsortlist->left) //leftsortlist已经到最后了,再回到第一个结点leftsortlist=leftsortlist->left;return leftsortlist;}
};