//肯定是要用中序遍历。。。可是开始不怎么会弄 //为什么这么菜 /* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } };*/ class Solution { public: void ConvertSup(TreeNode* &proot,TreeNode* &pre) { ConvertSup(proot->left,pre); proot->left=pre; if(pre!=NULL) pre->right=proot; pre=proot; ConvertSup(proot->right,pre); } TreeNode* Convert(TreeNode* pRootOfTree) { TreeNode* pre=NULL; if(pRootOfTree==NULL) { return NULL; } ConvertSup(pRootOfTree,pre); TreeNode* cur=pRootOfTree; while(cur->left!=NULL) { cur=cur->left; } return cur; } };
时间: 2024-10-13 14:16:05