## Cattle off - the next node binary tree

Title Description
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.
Solution: preorder left-right
two cases:
①pNode right subtree is empty, then the back up until it finds the node's left child node and the current node is the same node, the node i.e. pNode for the next node
in the right subtree ②pNode is not empty, then traverse down to find the right node pNode the left leaf node

``````class Solution {
public:
{
if (pNode == NULL || (pNode->left==NULL && pNode->right==NULL && pNode->next==NULL))
return NULL;
if (pNode->right == NULL)
{
while (pNode->next && pNode->next->left != pNode)
pNode = pNode->next;
return pNode->next;
}
else if (pNode->right)
{
pNode = pNode->right;
while (pNode->left)
pNode = pNode->left;
return pNode;
}
}
};
``````
Published 315 original articles · won praise 119 · views 110 000 +

### Guess you like

Origin blog.csdn.net/w144215160044/article/details/104917890
Recommended
Ranking
Daily