题解 | #二叉树的下一个结点#
二叉树的下一个结点
http://www.nowcoder.com/practice/9023a0c988684a53960365b889ceaf5e
static struct TreeLinkNode *pre = NULL;
static struct TreeLinkNode *ret = NULL;
//中序遍历,结果存入ret
void Inorder(struct TreeLinkNode *root, int val)
{
if (!root || ret)
return;
Inorder(root->left, val);
if (pre && pre->val == val)
ret = root;
pre = root;
Inorder(root->right, val);
}
struct TreeLinkNode *GetNext(struct TreeLinkNode *pNode)
{
struct TreeLinkNode *root;
root = pNode;
while (root->next)
root = root->next;
Inorder(root, pNode->val);
return ret;
}