标签:++ last list offer treenode 不能 describe null 实现
采用中序遍历遍历二叉树,利用二叉排序树的特性,顺次连接节点,形成双向链表。
/* 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* pRootOfTree) { //指向根节点左子树的最后一个节点 TreeNode *pLastNodeInList=NULL; ConvertNode(pRootOfTree ,&pLastNodeInList); TreeNode * pHeadFirst=pLastNodeInList; //记录根节点的值 while(pHeadFirst!=NULL && pHeadFirst->left!=NULL){ pHeadFirst=pHeadFirst->left; } return pHeadFirst; } void ConvertNode(TreeNode* pRootOfTree ,TreeNode** pLastNodeInlist){ if (pRootOfTree ==NULL){ return ; } TreeNode * cur=pRootOfTree; if (cur->left!=NULL){ ConvertNode(cur->left,pLastNodeInlist); } cur->left=*pLastNodeInlist; if(*pLastNodeInlist!=NULL){ (*pLastNodeInlist)->right=cur; } (*pLastNodeInlist)=cur; if (cur->right!=NULL){ ConvertNode(cur->right,pLastNodeInlist); } } };
标签:++ last list offer treenode 不能 describe null 实现
原文地址:https://www.cnblogs.com/fancy-li/p/11724935.html