二叉搜索树与双向链表
- 题目描述
- 算法分析
- 编程代码
链接: 二叉搜索树与双向链表
题目描述
算法分析
编程代码
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
void InOrder(TreeNode* root,TreeNode*& pre){
if(root == nullptr)
{
return;
}
InOrder(root->left,pre);
root->left = pre;
if(pre)
{
pre->right = root;
}
pre = root;
InOrder(root->right,pre);
}
TreeNode* Convert(TreeNode* pRootOfTree) {
if(pRootOfTree == nullptr)
{
return pRootOfTree;
}
TreeNode* pre = nullptr;
InOrder(pRootOfTree,pre);
while(pRootOfTree->left)
{
pRootOfTree = pRootOfTree->left;
}
return pRootOfTree;
}
};