Q: Given a binary tree and a node which is, find the next node in a preorder traversal order and returns. Note that the node in the tree contains not only the left and right child nodes, the parent node contains a pointer pointing to.

T:

Analysis of a node of binary tree, a total of the following cases:

1. The binary tree is empty, returning null;

2. right child node exists, a pointer is set from the right child node, along point to the left child node has been leaf node is the pointer to find the next node point;

3. node is not the root node. If the node is left child of its parent node, the parent node is returned; otherwise, continue to traverse up the parent node of its parent node, before the judge repeated, returns the result.

```
struct TreeLinkNode {
int val;
struct TreeLinkNode *left;
struct TreeLinkNode *right;
struct TreeLinkNode *next;
TreeLinkNode(int x) :val(x), left(NULL), right(NULL), next(NULL) {
}
};
TreeLinkNode* GetNext(TreeLinkNode* pNode)
{
if (pNode == nullptr)
return nullptr;
if (pNode->right != nullptr) {
TreeLinkNode *temp = pNode->right;
while (temp->left != nullptr)
temp = temp->left;
return temp;
}
while (pNode->next != nullptr) {
TreeLinkNode *temp = pNode->next;
if (pNode == temp->left)
return temp;
pNode = pNode->next;
}
return nullptr;
}
```