题解 | #二叉搜索树与双向链表#
二叉搜索树与双向链表
http://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
/**
*
* @param pRootOfTree TreeNode类
* @return TreeNode类
*/
static struct TreeNode *pre;
//这里需要加static才能正确定义
void Inorder(struct TreeNode *pRoot)
{
if (!pRoot)
return;
Inorder(pRoot->left);
pRoot->left = pre;
if (pre)
pre->right = pRoot;
pre = pRoot;
Inorder(pRoot->right);
}
//中序遍历
struct TreeNode *Convert(struct TreeNode *pRootOfTree)
{
struct TreeNode *p;
p = pRootOfTree;
if (p)
{
while (p->left)
p = p->left;
pre = p->left;
Inorder(pRootOfTree);
}
return p;
}