标签:mic 搜索 img convert png The nod 题目 info
题目:
解答:
1 // 中序遍历即可。只需要记录一个pre指针即可。 2 3 4 class Solution { 5 public: 6 TreeNode* Convert(TreeNode* pRootOfTree) 7 { 8 if(pRootOfTree == nullptr) 9 { 10 return nullptr; 11 } 12 13 TreeNode* pre = nullptr; 14 15 convertHelper(pRootOfTree, pre); 16 17 TreeNode* res = pRootOfTree; 18 while(res ->left) 19 { 20 res = res ->left; 21 } 22 return res; 23 } 24 25 void convertHelper(TreeNode* cur, TreeNode*& pre) 26 { 27 if(cur == nullptr) 28 { 29 return; 30 } 31 32 convertHelper(cur ->left, pre); 33 34 cur ->left = pre; 35 if(pre) 36 { 37 pre ->right = cur; 38 } 39 pre = cur; 40 41 convertHelper(cur ->right, pre); 42 } 43 };
标签:mic 搜索 img convert png The nod 题目 info
原文地址:https://www.cnblogs.com/ocpc/p/12858319.html