题解 | #二叉搜索树与双向链表#
二叉搜索树与双向链表
http://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5
class Solution {
public:
TreeNode* Convert(TreeNode* pRootOfTree)
{
if (pRootOfTree == nullptr)
return pRootOfTree;
auto pin = pRootOfTree;
Inorder(pin);
ivec.front()->left = nullptr;
ivec.back()->right = nullptr;
int length = ivec.size();
for (int i = 0; i != length-1; ++i){
ivec[i]->right = ivec[i+1];
ivec[i+1]->left = ivec[i];
}
return ivec.front();
}
void Inorder(TreeNode* T){
if(T != nullptr){
Inorder(T -> left);
ivec.push_back(T);
Inorder(T -> right);
}
}
private:
vector<TreeNode*> ivec;
};